Skip to content

Defer reading thread information in Process on Linux - #137497

Open
martincostello wants to merge 4 commits into
dotnet:mainfrom
martincostello:process-linux-defer-thread-info
Open

martincostello wants to merge 4 commits into
dotnet:mainfrom
martincostello:process-linux-defer-thread-info

Conversation

@martincostello

@martincostello martincostello commented Nov 13, 2026 •

Copy link
Copy Markdown
Member

Split out of #136630.

Summary

Creating the ProcessInfo that backs Process.WorkingSet64, Process.VirtualMemorySize64, Process.HandleCount, Process.StartTime etc. on Linux reads /proc/<pid>/task/<tid>/stat for every thread of the process, even though that information is only needed when Process.Threads is used. The cost (and allocations) scale with the number of threads, and are also paid for every process by Process.GetProcesses().

This changes it so the thread information is only read when Process.Threads is used.

  • ProcessInfo records whether its threads have been read (ThreadsPopulated) so that other platforms, which populate the information up front, are unaffected.
  • The deferred read uses the procfs ID (ProcPid) that was used to create the ProcessInfo, so it still works when the pid namespace of /proc does not match that of the process.
  • The deferred read is skipped if the process has exited, as its pid may have been recycled in the meantime. In that case Threads is empty.

Behavior

The observable difference is timing: previously the thread list was a snapshot taken when the process information was first read; now it is a snapshot taken when Threads is first accessed (and still reset by Refresh()). If the process has exited by then, Threads is empty rather than containing the threads that existed when other information was first read.

Tests

Adds Linux tests covering Threads being available after other process information was read (current and other process), and being empty when first accessed after the process exited. They have not been run locally (Linux-only, authored on Windows), so this relies on CI for execution.

Note

This pull request description and the changes were generated with AI assistance (Claude Code).

Creating the ProcessInfo that backs Process.WorkingSet64,
Process.VirtualMemorySize64, Process.HandleCount, Process.StartTime etc.
on Linux read /proc/<pid>/task/<tid>/stat for every thread of the process,
even though that information is only needed when Process.Threads is used.
The cost (and allocations) of that scale with the number of threads, and
are also paid for every process by Process.GetProcesses().

Only read the thread information when Process.Threads is used. The
ProcessInfo records whether the threads have been read so that other
platforms, which populate the information up front, are unaffected.

The deferred read uses the procfs identifier that was used to create the
ProcessInfo, so it still works when the pid namespace of /proc does not
match the one of the process, and it is skipped if the process has exited
as its pid may have been recycled in the meantime.

Assisted-by: Claude Code <[email protected]>
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Nov 13, 2026
@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.

@martincostello
martincostello marked this pull request as ready for review November 13, 2026 13:06
Copilot AI balanced review requested due to automatic review settings November 13, 2026 13:06

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Deferred thread enumeration does not validate the original process identity, so PID reuse can cause a retained non-child process to return a replacement process’s threads.

1 open issue
What changed in this PR

This PR separates deferred Linux thread enumeration from the HandleCount optimization in #136630, avoiding per-thread procfs reads when callers only need other process information.

Changes:

  • Populate thread information on first access to Process.Threads, rather than when creating ProcessInfo.
  • Preserve the procfs identifier and track whether threads have been populated.
  • Add Linux tests for deferred access, refresh, and access after an observed process exit.
File Description
src/​libraries/​System.Diagnostics.Process/​tests/​ProcessTests.Unix.cs Adds Linux deferred-thread tests.
src/​libraries/​System.Diagnostics.Process/​src/​System/​Diagnostics/​ProcessManager.Linux.cs Separates thread enumeration from process-information creation.
src/​libraries/​System.Diagnostics.Process/​src/​System/​Diagnostics/​ProcessInfo.Linux.cs Stores Linux-specific deferred-read state.
src/​libraries/​System.Diagnostics.Process/​src/​System/​Diagnostics/​ProcessInfo.cs Makes ProcessInfo partial for platform-specific state.
src/​libraries/​System.Diagnostics.Process/​src/​System/​Diagnostics/​Process.Linux.cs Adds synchronized deferred population with a cached exit-state check.
src/​libraries/​System.Diagnostics.Process/​src/​System/​Diagnostics/​Process.cs Invokes deferred population from Threads.
src/​libraries/​System.Diagnostics.Process/​src/​System.Diagnostics.Process.csproj Includes the Linux-specific ProcessInfo file.

Review effort: Balanced

Address GitHub Copilot review feedback.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

The cached-only exit check unnecessarily allocates a finalizable wait-state holder and acquires shared dictionary locks when reading Threads on a process without an existing holder.

1 open issue
1 previously missed issue

In code that hasn't changed since last review

Medium severity Avoid allocating a wait-state holder for cached-only exit checks

src/​libraries/​System.Diagnostics.Process/​src/​System/​Diagnostics/​Process.Linux.cs:138

For a process instance without a wait-state holder, this cached-only exit check calls GetWaitState(), allocating a finalizable ProcessWaitState.Holder and taking shared dictionary locks just to read Threads. Skip the shortcut when _waitStateHolder is null, following ThrowIfExited(false) in Process.Unix.cs:405. PopulateThreads still checks process identity before and after enumeration, so that guard remains in place.

Review effort: Balanced

Address further feedback from GitHub Copilot.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

The Linux-only changes to Process.Threads snapshot timing and post-exit behavior need maintainer compatibility review and Linux runtime and performance validation, which were not completed in this source-only review.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-System.Diagnostics.Process community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants