Repository navigation
Fix fork/exec race in ProcessTests.TestId and TestProcessName - #137530
Merged
Merged
Conversation
On Linux, Process.Start can return before the child's execve has updated /proc/<pid>/stat comm and cmdline, so the child can briefly report the forking thread's name. Retry briefly with Refresh(), matching the fix for ProcessStart_OnLinux_UsesSpecifiedProgram in #125918. Co-authored-by: Copilot App <[email protected]>
|
Azure Pipelines: Successfully started running 3 pipeline(s). 12 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
Contributor
|
Tagging subscribers to this area: @dotnet/area-system-diagnostics-process |
lewing
enabled auto-merge (squash)
November 14, 2026 01:13
steveisok
approved these changes
Nov 14, 2026
Member
|
/ba-g #137534 opened on the unrelated test failure |
This was referenced Nov 14, 2026
lewing
added a commit
that referenced
this pull request
Nov 14, 2026
## Problem #137530 (and earlier #125918) fixed the Linux fork/exec name-publication race in `ProcessTests.TestId`, `TestProcessName`, and `ProcessStart_OnLinux_UsesSpecifiedProgram*`. `Process.Start` returns when the child closes the `O_CLOEXEC` exec-status pipe, which happens before the kernel updates `comm` and `cmdline`. A `ProcessName` read right after `Process.Start` can therefore still see the forking .NET thread's name (for example `.NET TP Worker`). Other tests read a just-started child's name in the same way, and #137530 didn't change them: - `ProcessOpenTests.GetProcessById_RunningProcess_ReturnsProcess` compares two independently cached `ProcessName` values. In #137544, one read returned `.NET TP Worker` and the other `dotnet`. - `ProcessStart_UseShellExecute_OnUnix_SuccessWhenProgramInstalled` (OuterLoop, Linux) asserts `px.ProcessName` right after start. - `LongProcessNamesAreSupported` works around the race with `Thread.Sleep(100)`. - `ProcessNameMatchesScriptName` has been disabled under #13757. Its failures (an empty `/proc/<pid>/cmdline`, more frequent with NativeAOT) match this race. ## Fix This is a test-only change that uses the same pattern as #137530. Each assertion on a just-started child's name now runs inside `RetryHelper.Execute`, and calls `Refresh()` where the `Process` instance caches the name. The assertions themselves haven't changed, so a real failure still fails the test once the retries run out. `ProcessNameMatchesScriptName` is re-enabled. Its `/proc` reads and its `ProcessName` assertion are retried together. The test is now `[PlatformSpecific(TestPlatforms.Linux)]` because it reads `/proc/<pid>/stat` and `/proc/<pid>/cmdline`. This replaces the old macOS/iOS/tvOS skips and also keeps it off Browser, WASI, Android, and FreeBSD. ## Validation On macOS arm64, after `./build.sh clr+libs -rc release` (succeeded): - `dotnet build -t:Test src/libraries/System.Diagnostics.Process/tests/System.Diagnostics.Process.Tests.csproj "/p:XUnitOptions=-class System.Diagnostics.Tests.ProcessOpenTests -class System.Diagnostics.Tests.ProcessTests"` passed (Total: 227, Failed: 0, Skipped: 5) The Linux-only tests changed here, `ProcessNameMatchesScriptName` and `ProcessStart_UseShellExecute_OnUnix_SuccessWhenProgramInstalled`, don't run on macOS, and `LongProcessNamesAreSupported` is ActiveIssue'd on macOS. I didn't reproduce the Linux race locally. Resolves #13757 Resolves #137544 > [!NOTE] > This PR description and change were generated with the assistance of GitHub Copilot. Co-authored-by: Copilot App <[email protected]>
lewing
added a commit
that referenced
this pull request
Nov 16, 2026
## Problem
`ProcessTests.TestId` and `ProcessTests.TestProcessName` start a
long-running RemoteExecutor child and read its name right away. On
Linux, `Process.Start` returns when the child closes the `O_CLOEXEC`
exec-status pipe. In the kernel's `begin_new_exec`, that close happens
before `exec_mmap()` and `__set_task_comm()`, and before the ELF loader
sets `mm->arg_start/arg_end`. So for a short time `/proc/<pid>/stat`
comm still holds the name of the forking .NET thread (for example `.NET
TP Worker`), and cmdline still holds the parent's argv or is empty.
`GetUntruncatedProcessName` then falls back to the stale comm:
- `TestId`: the child pid is missing from
`Process.GetProcessesByName("dotnet")` (for example `Collection: [17,
3610] Not found: 3619`).
- `TestProcessName`: `Path.GetFileNameWithoutExtension(".NET TP
Worker")` is `""`, which produces the `Expected: dotnet / Actual: `
(empty) failures.
`TestProcessName` has failed occasionally since 2025. On 2026-11-13 both
tests started failing almost every run on containerized Helix legs
(Alpine 3.24 x64/arm64/arm32 on AzureLinux.4 hosts, Debian 13 arm32
jitstress). We haven't identified what made the race this frequent, but
the race itself has always been in these tests.
## Fix
This is a test-only change that follows #125918, which fixed the same
race in `ProcessStart_OnLinux_UsesSpecifiedProgram*`. Each assertion now
runs inside `RetryHelper.Execute`. `TestProcessName` calls
`_process.Refresh()` on each attempt so the cached name is re-read. The
assertions and their failure messages haven't changed, so a real failure
still fails the test after the retries run out. The Windows branch of
`TestId` is unchanged.
## Validation
On macOS arm64, after `./build.sh clr+libs -rc release` (succeeded):
- `dotnet build -t:Test
src/libraries/System.Diagnostics.Process/tests/System.Diagnostics.Process.Tests.csproj
"/p:XUnitOptions=-method System.Diagnostics.Tests.ProcessTests.TestId
-method System.Diagnostics.Tests.ProcessTests.TestProcessName"` passed
(Total: 2, Failed: 0)
- `dotnet build -t:Test
src/libraries/System.Diagnostics.Process/tests/System.Diagnostics.Process.Tests.csproj
"/p:XUnitOptions=-class System.Diagnostics.Tests.ProcessTests"` passed
(Total: 213, Failed: 0, Skipped: 4)
I didn't reproduce the Linux race locally. It depends on Linux `/proc`
timing, and these runs only show that nothing regressed on macOS.
Relates to #116051 (the Known Build Error for the
`TestProcessName` failures).
Resolves #137524
> [!NOTE]
> This PR description and change were generated with the assistance of
GitHub Copilot.
Co-authored-by: Copilot App <[email protected]>
(cherry picked from commit 4061a95)
lewing
added a commit
that referenced
this pull request
Nov 16, 2026
race in `ProcessTests.TestId`, `TestProcessName`, and `ProcessStart_OnLinux_UsesSpecifiedProgram*`. `Process.Start` returns when the child closes the `O_CLOEXEC` exec-status pipe, which happens before the kernel updates `comm` and `cmdline`. A `ProcessName` read right after `Process.Start` can therefore still see the forking .NET thread's name (for example `.NET TP Worker`). Other tests read a just-started child's name in the same way, and - `ProcessOpenTests.GetProcessById_RunningProcess_ReturnsProcess` compares two independently cached `ProcessName` values. In #137544, one read returned `.NET TP Worker` and the other `dotnet`. - `ProcessStart_UseShellExecute_OnUnix_SuccessWhenProgramInstalled` (OuterLoop, Linux) asserts `px.ProcessName` right after start. - `LongProcessNamesAreSupported` works around the race with `Thread.Sleep(100)`. - `ProcessNameMatchesScriptName` has been disabled under #13757. Its failures (an empty `/proc/<pid>/cmdline`, more frequent with NativeAOT) match this race. This is a test-only change that uses the same pattern as #137530. Each assertion on a just-started child's name now runs inside `RetryHelper.Execute`, and calls `Refresh()` where the `Process` instance caches the name. The assertions themselves haven't changed, so a real failure still fails the test once the retries run out. `ProcessNameMatchesScriptName` is re-enabled. Its `/proc` reads and its `ProcessName` assertion are retried together. The test is now `[PlatformSpecific(TestPlatforms.Linux)]` because it reads `/proc/<pid>/stat` and `/proc/<pid>/cmdline`. This replaces the old macOS/iOS/tvOS skips and also keeps it off Browser, WASI, Android, and FreeBSD. On macOS arm64, after `./build.sh clr+libs -rc release` (succeeded): - `dotnet build -t:Test src/libraries/System.Diagnostics.Process/tests/System.Diagnostics.Process.Tests.csproj "/p:XUnitOptions=-class System.Diagnostics.Tests.ProcessOpenTests -class System.Diagnostics.Tests.ProcessTests"` passed (Total: 227, Failed: 0, Skipped: 5) The Linux-only tests changed here, `ProcessNameMatchesScriptName` and `ProcessStart_UseShellExecute_OnUnix_SuccessWhenProgramInstalled`, don't run on macOS, and `LongProcessNamesAreSupported` is ActiveIssue'd on macOS. I didn't reproduce the Linux race locally. Resolves #13757 Resolves #137544 > [!NOTE] > This PR description and change were generated with the assistance of GitHub Copilot. Co-authored-by: Copilot App <[email protected]> [release/10.0 backport] Dropped the ProcessOpenTests.cs hunk: that file and GetProcessById_RunningProcess_ReturnsProcess(tryGet) do not exist on release/10.0. (cherry picked from commit b34b563)
steveisok
pushed a commit
that referenced
this pull request
Nov 16, 2026
## Problem
`ProcessTests.TestId` and `ProcessTests.TestProcessName` start a
long-running RemoteExecutor child and read its name right away. On
Linux, `Process.Start` returns when the child closes the `O_CLOEXEC`
exec-status pipe. In the kernel's `begin_new_exec`, that close happens
before `exec_mmap()` and `__set_task_comm()`, and before the ELF loader
sets `mm->arg_start/arg_end`. So for a short time `/proc/<pid>/stat`
comm still holds the name of the forking .NET thread (for example `.NET
TP Worker`), and cmdline still holds the parent's argv or is empty.
`GetUntruncatedProcessName` then falls back to the stale comm:
- `TestId`: the child pid is missing from
`Process.GetProcessesByName("dotnet")` (for example `Collection: [17,
3610] Not found: 3619`).
- `TestProcessName`: `Path.GetFileNameWithoutExtension(".NET TP
Worker")` is `""`, which produces the `Expected: dotnet / Actual: `
(empty) failures.
`TestProcessName` has failed occasionally since 2025. On 2026-11-13 both
tests started failing almost every run on containerized Helix legs
(Alpine 3.24 x64/arm64/arm32 on AzureLinux.4 hosts, Debian 13 arm32
jitstress). We haven't identified what made the race this frequent, but
the race itself has always been in these tests.
## Fix
This is a test-only change that follows #125918, which fixed the same
race in `ProcessStart_OnLinux_UsesSpecifiedProgram*`. Each assertion now
runs inside `RetryHelper.Execute`. `TestProcessName` calls
`_process.Refresh()` on each attempt so the cached name is re-read. The
assertions and their failure messages haven't changed, so a real failure
still fails the test after the retries run out. The Windows branch of
`TestId` is unchanged.
## Validation
On macOS arm64, after `./build.sh clr+libs -rc release` (succeeded):
- `dotnet build -t:Test
src/libraries/System.Diagnostics.Process/tests/System.Diagnostics.Process.Tests.csproj
"/p:XUnitOptions=-method System.Diagnostics.Tests.ProcessTests.TestId
-method System.Diagnostics.Tests.ProcessTests.TestProcessName"` passed
(Total: 2, Failed: 0)
- `dotnet build -t:Test
src/libraries/System.Diagnostics.Process/tests/System.Diagnostics.Process.Tests.csproj
"/p:XUnitOptions=-class System.Diagnostics.Tests.ProcessTests"` passed
(Total: 213, Failed: 0, Skipped: 4)
I didn't reproduce the Linux race locally. It depends on Linux `/proc`
timing, and these runs only show that nothing regressed on macOS.
Relates to #116051 (the Known Build Error for the
`TestProcessName` failures).
Resolves #137524
> [!NOTE]
> This PR description and change were generated with the assistance of
GitHub Copilot.
Co-authored-by: Copilot App <[email protected]>
(cherry picked from commit 4061a95)
This was referenced Nov 20, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
ProcessTests.TestIdandProcessTests.TestProcessNamestart a long-running RemoteExecutor child and read its name right away. On Linux,Process.Startreturns when the child closes theO_CLOEXECexec-status pipe. In the kernel'sbegin_new_exec, that close happens beforeexec_mmap()and__set_task_comm(), and before the ELF loader setsmm->arg_start/arg_end. So for a short time/proc/<pid>/statcomm still holds the name of the forking .NET thread (for example.NET TP Worker), and cmdline still holds the parent's argv or is empty.GetUntruncatedProcessNamethen falls back to the stale comm:TestId: the child pid is missing fromProcess.GetProcessesByName("dotnet")(for exampleCollection: [17, 3610] Not found: 3619).TestProcessName:Path.GetFileNameWithoutExtension(".NET TP Worker")is"", which produces theExpected: dotnet / Actual:(empty) failures.TestProcessNamehas failed occasionally since 2025. On 2026-11-13 both tests started failing almost every run on containerized Helix legs (Alpine 3.24 x64/arm64/arm32 on AzureLinux.4 hosts, Debian 13 arm32 jitstress). We haven't identified what made the race this frequent, but the race itself has always been in these tests.Fix
This is a test-only change that follows #125918, which fixed the same race in
ProcessStart_OnLinux_UsesSpecifiedProgram*. Each assertion now runs insideRetryHelper.Execute.TestProcessNamecalls_process.Refresh()on each attempt so the cached name is re-read. The assertions and their failure messages haven't changed, so a real failure still fails the test after the retries run out. The Windows branch ofTestIdis unchanged.Validation
On macOS arm64, after
./build.sh clr+libs -rc release(succeeded):dotnet build -t:Test src/libraries/System.Diagnostics.Process/tests/System.Diagnostics.Process.Tests.csproj "/p:XUnitOptions=-method System.Diagnostics.Tests.ProcessTests.TestId -method System.Diagnostics.Tests.ProcessTests.TestProcessName"passed (Total: 2, Failed: 0)dotnet build -t:Test src/libraries/System.Diagnostics.Process/tests/System.Diagnostics.Process.Tests.csproj "/p:XUnitOptions=-class System.Diagnostics.Tests.ProcessTests"passed (Total: 213, Failed: 0, Skipped: 4)I didn't reproduce the Linux race locally. It depends on Linux
/proctiming, and these runs only show that nothing regressed on macOS.Relates to #116051 (the Known Build Error for the
TestProcessNamefailures).Resolves #137524
Note
This PR description and change were generated with the assistance of GitHub Copilot.