diff --git a/DevProxy.Integration.Tests/ProcessSmokeTests.cs b/DevProxy.Integration.Tests/ProcessSmokeTests.cs index c89f2d522..479dd3599 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); } } } @@ -521,7 +521,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. } @@ -591,7 +591,7 @@ await process.StandardInput.WriteAsync( } finally { - workDir.Delete(recursive: true); + DeleteWorkDir(workDir); } } @@ -650,7 +650,7 @@ await File.WriteAllTextAsync(Path.Combine(workDir.FullName, "devproxyrc.json"), } finally { - workDir.Delete(recursive: true); + DeleteWorkDir(workDir); } } @@ -735,6 +735,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