From 45634f4105a1c65bc52e7bd4edd6e1e691c3442f Mon Sep 17 00:00:00 2001 From: Ilia Petrov-Komotskii Date: Mon, 23 Nov 2026 16:17:23 +0100 Subject: [PATCH] fix: fixing Windows CI tests issues --- .../Windows/WindowsPlatform.Processes.cs | 21 ++++++--- .../WindowsPlatformTests.cs | 43 +++++++++++++++---- 2 files changed, 49 insertions(+), 15 deletions(-) diff --git a/src/ManeFfmpeg.Core/Platforms/Windows/WindowsPlatform.Processes.cs b/src/ManeFfmpeg.Core/Platforms/Windows/WindowsPlatform.Processes.cs index 12c72b5..1cf088d 100644 --- a/src/ManeFfmpeg.Core/Platforms/Windows/WindowsPlatform.Processes.cs +++ b/src/ManeFfmpeg.Core/Platforms/Windows/WindowsPlatform.Processes.cs @@ -42,14 +42,23 @@ public CommandTask StartPausable(string executable, IReadOnlyList /// public IFrozenProcess Freeze(int pid) { - var tree = ProcessTree(pid); - foreach (var id in tree) + // A process can start a child after the tree is read and before it is suspended itself, so the tree is read + // again until it holds nothing new: once every process in it is suspended, none can start another. + var frozen = new List(); + for (var pass = 0; pass < 10; pass++) { - // A shim's child may have started before the shim joined the job. - AddToJob(id); - Call(id, NtSuspendProcess, "suspend"); + var added = ProcessTree(pid).Where(id => !frozen.Contains(id)).ToList(); + if (added.Count == 0) + break; + foreach (var id in added) + { + // A shim's child may have started before the shim joined the job. + AddToJob(id); + Call(id, NtSuspendProcess, "suspend"); + frozen.Add(id); + } } - return new SuspendedProcesses([.. tree.Select(id => (id, StartTime(id)))]); + return new SuspendedProcesses([.. frozen.Select(id => (id, StartTime(id)))]); } private sealed class SuspendedProcesses(IReadOnlyList<(int Id, DateTime? Started)> tree) : IFrozenProcess diff --git a/tests/ManeFfmpeg.Core.Tests/WindowsPlatformTests.cs b/tests/ManeFfmpeg.Core.Tests/WindowsPlatformTests.cs index 7899fcb..5aeebc0 100644 --- a/tests/ManeFfmpeg.Core.Tests/WindowsPlatformTests.cs +++ b/tests/ManeFfmpeg.Core.Tests/WindowsPlatformTests.cs @@ -2,6 +2,7 @@ using System.Text; using CliWrap; using CliWrap.Buffered; +using ManeFfmpeg.Core.Persistence; using ManeFfmpeg.Core.Platforms; using ManeFfmpeg.Core.Platforms.Windows; @@ -297,23 +298,24 @@ public async Task A_frozen_process_and_its_children_are_suspended_until_resumed( using var process = Process.Start(new ProcessStartInfo("cmd.exe", "/c ping -n 30 127.0.0.1 >nul") { CreateNoWindow = true })!; try { - IReadOnlyList tree = []; - for (var i = 0; i < 50 && tree.Count < 2; i++) + // The tree also holds cmd.exe's conhost.exe, which may come first: wait for ping itself. + int? ping = null; + for (var i = 0; i < 50 && ping is null; i++) { await Task.Delay(100); - tree = WindowsPlatform.ProcessTree(process.Id); + ping = WindowsPlatform.ProcessTree(process.Id).Skip(1).FirstOrDefault(id => NameOf(id) == "PING"); } - Assert.True(tree.Count >= 2, "ping didn't start"); - Assert.Equal(process.Id, tree[0]); + Assert.True(ping is not null, "ping didn't start"); + // The shim and its real child; conhost.exe is Windows' console host, not part of the export's work. // A thread that is already blocked (cmd.exe waiting for ping) shows as suspended only once the kernel has // delivered the suspend to it, a moment after NtSuspendProcess returns; likewise for resuming. var frozen = Windows.Freeze(process.Id); - foreach (var id in tree) - await Wait.Until(() => IsSuspended(id), $"pid {id} to be suspended"); + foreach (var id in new[] { process.Id, ping.Value }) + await Wait.Until(() => IsSuspended(id), $"pid {id} to be suspended", () => Threads(id)); frozen.Resume(); - foreach (var id in tree) - await Wait.Until(() => !IsSuspended(id), $"pid {id} to be resumed"); + foreach (var id in new[] { process.Id, ping.Value }) + await Wait.Until(() => !IsSuspended(id), $"pid {id} to be resumed", () => Threads(id)); } finally { @@ -363,6 +365,29 @@ private static bool IsRunning(int pid) } } + private static string? NameOf(int pid) + { + try + { + using var process = Process.GetProcessById(pid); + return process.ProcessName.ToUpperInvariant(); + } + catch (Exception ex) when (ex is ArgumentException or InvalidOperationException) + { + return null; + } + } + + /// The process's name and each thread's state, and the app log (which says when a suspend call failed). + private static string Threads(int pid) + { + using var process = Process.GetProcessById(pid); + var threads = process.Threads.Cast().Select(t => + $"{t.Id} {t.ThreadState}{(t.ThreadState == System.Diagnostics.ThreadState.Wait ? $"/{t.WaitReason}" : "")}"); + var log = File.Exists(AppLog.FilePath) ? File.ReadAllText(AppLog.FilePath) : "(no app log)"; + return $"{process.ProcessName}: {string.Join(", ", threads)}\n{log}"; + } + private static bool IsSuspended(int pid) { using var process = Process.GetProcessById(pid);