From 8e9fd844c707b4cbded9ea7d605b99fa3b6b5655 Mon Sep 17 00:00:00 2001 From: Waldek Mastykarz Date: Thu, 26 Nov 2026 15:16:43 +0100 Subject: [PATCH] Retry deleting smoke test work folders on Windows A killed host's descendants (cmd.exe /c more in the stdio tests) can still hold the work folder as their working directory right after Kill returns, which made Stdio_BackToBackJsonRpcMessages_AreMockedIndividually fail intermittently on Windows. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- .../ProcessSmokeTests.cs | 35 ++++++++++++++++--- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/DevProxy.Integration.Tests/ProcessSmokeTests.cs b/DevProxy.Integration.Tests/ProcessSmokeTests.cs index 8f553cec1..d88f6fb47 100644 --- a/DevProxy.Integration.Tests/ProcessSmokeTests.cs +++ b/DevProxy.Integration.Tests/ProcessSmokeTests.cs @@ -191,7 +191,7 @@ public async Task Startup_EnvironmentOverrides_PreserveTildeConfigPathAndApplyBe { File.Delete(logFile); } - workDir.Delete(recursive: true); + DeleteWorkDir(workDir); } } } @@ -302,7 +302,7 @@ public async Task Status_HonorsInstanceTokenSuppression_ExplicitRetrievalRemains { File.Delete(logFile); } - workDir.Delete(recursive: true); + DeleteWorkDir(workDir); } } } @@ -400,7 +400,7 @@ public async Task Host_LoadsMockPluginFromConfig_AndServesMock() } finally { - workDir.Delete(recursive: true); + DeleteWorkDir(workDir); // The host writes a timestamped log to the working dir's parent in some modes; // the temp dir delete above covers logs written under workDir. Best-effort. } @@ -470,7 +470,7 @@ await process.StandardInput.WriteAsync( } finally { - workDir.Delete(recursive: true); + DeleteWorkDir(workDir); } } @@ -529,7 +529,7 @@ await File.WriteAllTextAsync(Path.Combine(workDir.FullName, "devproxyrc.json"), } finally { - workDir.Delete(recursive: true); + DeleteWorkDir(workDir); } } @@ -614,6 +614,31 @@ private static async Task WaitForListeningPortAsync(Process process) } } + // On Windows, a killed process can keep its working directory in use for a moment after + // Kill returns, and descendants such as the stdio child can't be waited for. + private static void DeleteWorkDir(DirectoryInfo workDir) + { + var deadline = DateTime.UtcNow.AddSeconds(10); + while (true) + { + workDir.Refresh(); + if (!workDir.Exists) + { + return; + } + + try + { + workDir.Delete(recursive: true); + return; + } + catch (Exception ex) when (ex is IOException or UnauthorizedAccessException && DateTime.UtcNow < deadline) + { + Thread.Sleep(200); + } + } + } + private static void StopHost(Process process) { try