diff --git a/src/MSBuild.UnitTests/MSBuildServer_Tests.cs b/src/MSBuild.UnitTests/MSBuildServer_Tests.cs index a820d138bf8..6272a874389 100644 --- a/src/MSBuild.UnitTests/MSBuildServer_Tests.cs +++ b/src/MSBuild.UnitTests/MSBuildServer_Tests.cs @@ -190,8 +190,11 @@ public void BuildsWhileBuildIsRunningOnServer() _env.WithTransientProcess(pidOfServerProcess); string? dir = Path.GetDirectoryName(markerFile.Path); - using var watcher = new System.IO.FileSystemWatcher(dir!); + // mre must be declared before watcher so that it is disposed after watcher. + // Reversing this order would allow late FileSystemWatcher callbacks to call + // mre.Set() on a disposed ManualResetEvent, causing an ObjectDisposedException. using ManualResetEvent mre = new ManualResetEvent(false); + using var watcher = new System.IO.FileSystemWatcher(dir!); watcher.Created += (o, e) => { _output.WriteLine($"The marker file {markerFile.Path} was created. The build task has been started.");