Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions src/Build/BackEnd/Client/MSBuildClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -265,9 +265,10 @@ private bool TryShutdownServer(CancellationToken cancellationToken)
return true;
}

// Check that server is not busy.
bool serverWasBusy = ServerWasBusy();
if (serverWasBusy)
// Check and wait for server to be not busy for some short time to avoid race condition when server reports build is finished but had not released ServerBusy mutex yet.
// If during that short time, a script would try to shutdown server, it would be rejected and server would continue to run.
bool serverIsBusy = ServerIsBusyWithWaitAndRetry(250);
if (serverIsBusy)
{
CommunicationsUtilities.Trace("Server cannot be shut down for it is not idle.");
return false;
Expand All @@ -291,6 +292,20 @@ private bool TryShutdownServer(CancellationToken cancellationToken)
return _exitResult.MSBuildClientExitType == MSBuildClientExitType.Success;
}

private bool ServerIsBusyWithWaitAndRetry(int milliseconds)
{
bool isBusy = ServerWasBusy();
Stopwatch sw = Stopwatch.StartNew();
while (isBusy && sw.ElapsedMilliseconds < milliseconds)
{
CommunicationsUtilities.Trace("Wait for server to be not busy - will retry soon...");
Thread.Sleep(100);
isBusy = ServerWasBusy();
}

return isBusy;
}

internal bool ServerIsRunning()
{
string serverRunningMutexName = OutOfProcServerNode.GetRunningServerMutexName(_handshake);
Expand Down
4 changes: 0 additions & 4 deletions src/MSBuild.UnitTests/MSBuildServer_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,6 @@ public void CanShutdownServerProcess(bool byBuildManager)
{
_env.SetEnvironmentVariable("MSBUILDUSESERVER", "1");

// This test seems to be flaky, lets enable better logging to investigate it next time
// TODO: delete after investigated its flakiness
_env.WithTransientDebugEngineForNewProcesses(true);

TransientTestFile project = _env.CreateFile("testProject.proj", printPidContents);

// Start a server node and find its PID.
Expand Down