From 159f3299deac1a69f2b402bf332340c100666226 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 18 Nov 2026 19:02:54 +0000 Subject: [PATCH 1/2] Stop dotnet restore hanging when MSBuild worker nodes inherit its output ProcessRunner waits for the child's redirected stdout/stderr to reach EOF. When MSBUILDENSURESTDOUTFORTASKPROCESSES=1 is inherited (dotnet test sets it), MSBuild launches worker nodes without redirecting their streams, so newly launched reusable nodes keep our pipes open after the build ends and the wait never completes. It hung MultiFileSolutionAndProjectTests intermittently, whenever restore had to launch new nodes. Disable node reuse for processes we run, so the nodes exit with the build. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_019FEr8FVAmHV9bscZ82CChg --- CodeConv/Util/ProcessRunner.cs | 13 +++++++ Tests/LanguageAgnostic/ProcessRunnerTests.cs | 37 ++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 Tests/LanguageAgnostic/ProcessRunnerTests.cs diff --git a/CodeConv/Util/ProcessRunner.cs b/CodeConv/Util/ProcessRunner.cs index 9070a93a8..99ca4da12 100644 --- a/CodeConv/Util/ProcessRunner.cs +++ b/CodeConv/Util/ProcessRunner.cs @@ -35,6 +35,7 @@ private static async Task GetExitCodeAsync(this ProcessStartInfo psi, TextW psi.UseShellExecute = false; psi.RedirectStandardError = true; psi.RedirectStandardOutput = true; + DisableMsBuildNodeReuse(psi); using var process = new Process { StartInfo = psi }; var stdOutComplete = new TaskCompletionSource(); var stdErrComplete = new TaskCompletionSource(); @@ -61,4 +62,16 @@ private static async Task GetExitCodeAsync(this ProcessStartInfo psi, TextW return process.ExitCode; } + + /// + /// We wait for the redirected stdout/stderr to close, which only happens once every process holding them has exited. + /// MSBuild keeps worker nodes alive after the build for reuse, and launches them without redirecting their streams when + /// MSBUILDENSURESTDOUTFORTASKPROCESSES=1 (which `dotnet test` sets for its children), so they inherit our pipes and we'd wait + /// until they time out. Without node reuse, the nodes exit with the build. + /// Streams only redirected when that variable isn't set: + /// https://github.com/dotnet/msbuild/blob/2bccd27c1733ff52893ff41e3b4046e51d0334ee/src/Build/BackEnd/Components/Communications/NodeLauncher.cs#L65-L67 + /// https://github.com/dotnet/msbuild/blob/2bccd27c1733ff52893ff41e3b4046e51d0334ee/src/Framework/Traits.cs#L487-L490 + /// MSBUILDDISABLENODEREUSE: https://github.com/dotnet/msbuild/blob/2bccd27c1733ff52893ff41e3b4046e51d0334ee/documentation/wiki/MSBuild-Environment-Variables.md + /// + private static void DisableMsBuildNodeReuse(ProcessStartInfo psi) => psi.Environment["MSBUILDDISABLENODEREUSE"] = "1"; } \ No newline at end of file diff --git a/Tests/LanguageAgnostic/ProcessRunnerTests.cs b/Tests/LanguageAgnostic/ProcessRunnerTests.cs new file mode 100644 index 000000000..a7a65257b --- /dev/null +++ b/Tests/LanguageAgnostic/ProcessRunnerTests.cs @@ -0,0 +1,37 @@ +using System; +using System.Diagnostics; +using System.IO; +using System.Threading.Tasks; +using ICSharpCode.CodeConverter.DotNetTool.Util; +using ICSharpCode.CodeConverter.Tests.TestRunners; +using Xunit; + +namespace ICSharpCode.CodeConverter.Tests.LanguageAgnostic; + +/// +/// In the MSBuild collection so the restore doesn't race the multi-file tests' restore of the same solution. +/// +[Collection(MultiFileTestFixture.Collection)] +public class ProcessRunnerTests +{ + private static readonly TimeSpan RestoreTimeout = TimeSpan.FromMinutes(3); + + [Fact] + public async Task GetOutputAsyncReturnsWhenRestoreLeavesNewWorkerNodesRunningAsync() + { + var solutionFile = Path.Combine(TestConstants.GetTestDataDirectory(), "MultiFileCharacterization", "SourceFiles", "CharacterizationTestSolution.sln"); + var psi = new ProcessStartInfo("dotnet", $"restore \"{solutionFile}\""); + psi.Environment["EnableWindowsTargeting"] = "true"; + // `dotnet test` sets this for its children: MSBuild then lets worker nodes inherit stdout/stderr instead of redirecting them + psi.Environment["MSBUILDENSURESTDOUTFORTASKPROCESSES"] = "1"; + // A unique handshake stops MSBuild reusing an idle node, so it must launch new ones + psi.Environment["MSBUILDNODEHANDSHAKESALT"] = Guid.NewGuid().ToString("N"); + + var restore = psi.GetOutputAsync(); + var completed = await Task.WhenAny(restore, Task.Delay(RestoreTimeout)); + + Assert.True(completed == restore, $"dotnet restore output didn't end within {RestoreTimeout}"); + var (exitCode, _, stdErr) = await restore; + Assert.True(exitCode == 0, stdErr); + } +} From 9a4c73affc6b2ef3ec19648972eba78d7d8b3697 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 18 Nov 2026 20:18:02 +0000 Subject: [PATCH 2/2] Make the worker-node regression test build two trivial projects instead of a restore A parallel build of two SDK-less projects is enough to make MSBuild launch a worker node, so the test no longer adds a second restore of the characterization solution to the run. It takes ~0.6s, needs no NuGet or SDK evaluation, and no longer needs the MSBuild collection since it uses its own temp directory. It still fails without MSBUILDDISABLENODEREUSE. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_019FEr8FpweGikrBKYdcSCS1 --- Tests/LanguageAgnostic/ProcessRunnerTests.cs | 47 ++++++++++++-------- 1 file changed, 28 insertions(+), 19 deletions(-) diff --git a/Tests/LanguageAgnostic/ProcessRunnerTests.cs b/Tests/LanguageAgnostic/ProcessRunnerTests.cs index a7a65257b..c21a7fbbd 100644 --- a/Tests/LanguageAgnostic/ProcessRunnerTests.cs +++ b/Tests/LanguageAgnostic/ProcessRunnerTests.cs @@ -3,35 +3,44 @@ using System.IO; using System.Threading.Tasks; using ICSharpCode.CodeConverter.DotNetTool.Util; -using ICSharpCode.CodeConverter.Tests.TestRunners; using Xunit; namespace ICSharpCode.CodeConverter.Tests.LanguageAgnostic; -/// -/// In the MSBuild collection so the restore doesn't race the multi-file tests' restore of the same solution. -/// -[Collection(MultiFileTestFixture.Collection)] public class ProcessRunnerTests { - private static readonly TimeSpan RestoreTimeout = TimeSpan.FromMinutes(3); + private static readonly TimeSpan BuildTimeout = TimeSpan.FromMinutes(1); + /// + /// Two SDK-less projects built in parallel make MSBuild launch a worker node, without the cost of a real restore. + /// [Fact] - public async Task GetOutputAsyncReturnsWhenRestoreLeavesNewWorkerNodesRunningAsync() + public async Task GetOutputAsyncReturnsWhenBuildLeavesNewWorkerNodesRunningAsync() { - var solutionFile = Path.Combine(TestConstants.GetTestDataDirectory(), "MultiFileCharacterization", "SourceFiles", "CharacterizationTestSolution.sln"); - var psi = new ProcessStartInfo("dotnet", $"restore \"{solutionFile}\""); - psi.Environment["EnableWindowsTargeting"] = "true"; - // `dotnet test` sets this for its children: MSBuild then lets worker nodes inherit stdout/stderr instead of redirecting them - psi.Environment["MSBUILDENSURESTDOUTFORTASKPROCESSES"] = "1"; - // A unique handshake stops MSBuild reusing an idle node, so it must launch new ones - psi.Environment["MSBUILDNODEHANDSHAKESALT"] = Guid.NewGuid().ToString("N"); + var dir = Directory.CreateTempSubdirectory("ProcessRunnerTests"); + try { + File.WriteAllText(Path.Combine(dir.FullName, "child.proj"), @""); + File.WriteAllText(Path.Combine(dir.FullName, "root.proj"), @" + +

+

+ + +"); + var psi = new ProcessStartInfo("dotnet", "msbuild root.proj -m:2 -nologo -v:q") { WorkingDirectory = dir.FullName }; + // `dotnet test` sets this for its children: MSBuild then lets worker nodes inherit stdout/stderr instead of redirecting them + psi.Environment["MSBUILDENSURESTDOUTFORTASKPROCESSES"] = "1"; + // A unique handshake stops MSBuild reusing an idle node, so it must launch a new one + psi.Environment["MSBUILDNODEHANDSHAKESALT"] = Guid.NewGuid().ToString("N"); - var restore = psi.GetOutputAsync(); - var completed = await Task.WhenAny(restore, Task.Delay(RestoreTimeout)); + var build = psi.GetOutputAsync(); + var completed = await Task.WhenAny(build, Task.Delay(BuildTimeout)); - Assert.True(completed == restore, $"dotnet restore output didn't end within {RestoreTimeout}"); - var (exitCode, _, stdErr) = await restore; - Assert.True(exitCode == 0, stdErr); + Assert.True(completed == build, $"dotnet msbuild output didn't end within {BuildTimeout}"); + var (exitCode, stdOut, stdErr) = await build; + Assert.True(exitCode == 0, stdOut + stdErr); + } finally { + dir.Delete(true); + } } }