From 5fa854609522d61674a01c2d13decf2360f62324 Mon Sep 17 00:00:00 2001 From: Tom Deseyn Date: Wed, 29 Apr 2026 16:43:10 +0200 Subject: [PATCH 1/3] ProcessTests: redirect stdio and kill entire process tree for shell-script-based tests. The LongProcessNamesAreSupported and ProcessNameMatchesScriptName tests spawn a shell script that runs sleep as a grandchild. Killing only the shell left the sleep process alive, holding stdout/stderr pipes open for the parent that runs the tests. --- .../tests/ProcessTests.Unix.cs | 5 +++-- .../System.Diagnostics.Process/tests/ProcessTests.cs | 9 +++++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/libraries/System.Diagnostics.Process/tests/ProcessTests.Unix.cs b/src/libraries/System.Diagnostics.Process/tests/ProcessTests.Unix.cs index 1e7399eba08ae0..828e4fc4d4a218 100644 --- a/src/libraries/System.Diagnostics.Process/tests/ProcessTests.Unix.cs +++ b/src/libraries/System.Diagnostics.Process/tests/ProcessTests.Unix.cs @@ -173,7 +173,8 @@ public void ProcessNameMatchesScriptName() File.WriteAllText(filename, $"#!/bin/sh\nsleep 600\n"); // sleep 10 min. File.SetUnixFileMode(filename, ExecutablePermissions); - using (var process = Process.Start(new ProcessStartInfo { FileName = filename })) + var psi = new ProcessStartInfo { FileName = filename, RedirectStandardOutput = true, RedirectStandardError = true }; + using (var process = Process.Start(psi)) { try { @@ -186,7 +187,7 @@ public void ProcessNameMatchesScriptName() } finally { - process.Kill(); + process.Kill(entireProcessTree: true); process.WaitForExit(); } } diff --git a/src/libraries/System.Diagnostics.Process/tests/ProcessTests.cs b/src/libraries/System.Diagnostics.Process/tests/ProcessTests.cs index 61dad866b71209..3c58b83cfd7f39 100644 --- a/src/libraries/System.Diagnostics.Process/tests/ProcessTests.cs +++ b/src/libraries/System.Diagnostics.Process/tests/ProcessTests.cs @@ -2499,7 +2499,12 @@ public void LongProcessNamesAreSupported() string sleepCommandPathFileName = Path.Combine(TestDirectory, LongProcessName); File.Copy(sleepPath, sleepCommandPathFileName); - using (Process px = Process.Start(sleepCommandPathFileName, "600")) + var psi = new ProcessStartInfo(sleepCommandPathFileName, "600") + { + RedirectStandardOutput = true, + RedirectStandardError = true, + }; + using (Process px = Process.Start(psi)) { // Reading of long process names is flaky during process startup and shutdown. // Wait a bit to skip over the period where the ProcessName is not reliable. @@ -2512,7 +2517,7 @@ public void LongProcessNamesAreSupported() } finally { - px.Kill(); + px.Kill(entireProcessTree: true); px.WaitForExit(); } } From 9bcd2c37c5e87faaad1411f3bbedb348630cc7da Mon Sep 17 00:00:00 2001 From: Tom Deseyn Date: Thu, 30 Apr 2026 11:49:47 +0200 Subject: [PATCH 2/3] Apply suggestions from code review Co-authored-by: Adam Sitnik --- .../System.Diagnostics.Process/tests/ProcessTests.Unix.cs | 7 ++++++- .../System.Diagnostics.Process/tests/ProcessTests.cs | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/libraries/System.Diagnostics.Process/tests/ProcessTests.Unix.cs b/src/libraries/System.Diagnostics.Process/tests/ProcessTests.Unix.cs index 828e4fc4d4a218..f91f394ddd4a55 100644 --- a/src/libraries/System.Diagnostics.Process/tests/ProcessTests.Unix.cs +++ b/src/libraries/System.Diagnostics.Process/tests/ProcessTests.Unix.cs @@ -173,7 +173,12 @@ public void ProcessNameMatchesScriptName() File.WriteAllText(filename, $"#!/bin/sh\nsleep 600\n"); // sleep 10 min. File.SetUnixFileMode(filename, ExecutablePermissions); - var psi = new ProcessStartInfo { FileName = filename, RedirectStandardOutput = true, RedirectStandardError = true }; + using SafeFileHandle nullHandle = File.OpenNullHandle(); + ProcessStartInfo psi = new(filename) + { + StandardOutputHandle = nullHandle, + StandardErrorHandle= nullHandle + }; using (var process = Process.Start(psi)) { try diff --git a/src/libraries/System.Diagnostics.Process/tests/ProcessTests.cs b/src/libraries/System.Diagnostics.Process/tests/ProcessTests.cs index 3c58b83cfd7f39..7853ed2eebaf8c 100644 --- a/src/libraries/System.Diagnostics.Process/tests/ProcessTests.cs +++ b/src/libraries/System.Diagnostics.Process/tests/ProcessTests.cs @@ -2499,7 +2499,7 @@ public void LongProcessNamesAreSupported() string sleepCommandPathFileName = Path.Combine(TestDirectory, LongProcessName); File.Copy(sleepPath, sleepCommandPathFileName); - var psi = new ProcessStartInfo(sleepCommandPathFileName, "600") + ProcessStartInfo psi = new(sleepCommandPathFileName, "600") { RedirectStandardOutput = true, RedirectStandardError = true, From 79d0d915f56457e170feb123ff8923f23e25a8f1 Mon Sep 17 00:00:00 2001 From: Adam Sitnik Date: Thu, 30 Apr 2026 12:17:55 +0200 Subject: [PATCH 3/3] Apply suggestions from code review Co-authored-by: Adam Sitnik --- .../System.Diagnostics.Process/tests/ProcessTests.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/libraries/System.Diagnostics.Process/tests/ProcessTests.cs b/src/libraries/System.Diagnostics.Process/tests/ProcessTests.cs index 7853ed2eebaf8c..afa66d916c2f5a 100644 --- a/src/libraries/System.Diagnostics.Process/tests/ProcessTests.cs +++ b/src/libraries/System.Diagnostics.Process/tests/ProcessTests.cs @@ -2499,10 +2499,11 @@ public void LongProcessNamesAreSupported() string sleepCommandPathFileName = Path.Combine(TestDirectory, LongProcessName); File.Copy(sleepPath, sleepCommandPathFileName); + using SafeFileHandle nullHandle = File.OpenNullHandle(); ProcessStartInfo psi = new(sleepCommandPathFileName, "600") { - RedirectStandardOutput = true, - RedirectStandardError = true, + StandardOutputHandle = nullHandle, + StandardErrorHandle= nullHandle }; using (Process px = Process.Start(psi)) {