Skip to content

Fix fork/exec race in ProcessTests.TestId and TestProcessName - #137530

Merged
lewing merged 1 commit into
mainfrom
lewing-fix-process-name-race-test
Nov 14, 2026
Merged

lewing merged 1 commit into
mainfrom
lewing-fix-process-name-race-test

Conversation

@lewing

@lewing lewing commented Nov 14, 2026

Copy link
Copy Markdown
Member

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.

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

Copy link
Copy Markdown
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.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/area-system-diagnostics-process
See info in area-owners.md if you want to be subscribed.

@lewing
lewing enabled auto-merge (squash) November 14, 2026 01:13
@lewing
lewing requested a review from jkotas November 14, 2026 01:15
@jkotas

jkotas commented Nov 14, 2026

Copy link
Copy Markdown
Member

/ba-g #137534 opened on the unrelated test failure

@lewing
lewing merged commit 4061a95 into main Nov 14, 2026
65 of 67 checks passed
@lewing
lewing deleted the lewing-fix-process-name-race-test branch November 14, 2026 03:04
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)
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Test failure: System.Diagnostics.Tests.ProcessTests.TestId on Alpine 3.24

3 participants