diff --git a/src/MSBuild.UnitTests/MSBuildServer_Tests.cs b/src/MSBuild.UnitTests/MSBuildServer_Tests.cs index 1531c665f54..1f196298a2d 100644 --- a/src/MSBuild.UnitTests/MSBuildServer_Tests.cs +++ b/src/MSBuild.UnitTests/MSBuildServer_Tests.cs @@ -253,16 +253,24 @@ public void CanShutdownServerProcess(bool byBuildManager) if (byBuildManager) { - BuildManager.DefaultBuildManager.ShutdownAllNodes(); + // ShutdownAllNodes does not expose whether the MSBuildServer was successfully + // contacted. The server may be recycling its pipe right after the build completes + // (busy mutex released before new pipe is ready), causing the first attempt to + // silently fail. Retry until the process exits or a reasonable timeout elapses. + Stopwatch sw = Stopwatch.StartNew(); + while (!serverProcess.HasExited && sw.ElapsedMilliseconds < 10_000) + { + BuildManager.DefaultBuildManager.ShutdownAllNodes(); + serverProcess.WaitForExit(500); + } } else { bool serverIsDown = MSBuildClient.ShutdownServer(CancellationToken.None); serverIsDown.ShouldBeTrue(); + serverProcess.WaitForExit(10_000); } - serverProcess.WaitForExit(10_000); - serverProcess.HasExited.ShouldBeTrue(); }