You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Defer reading thread information in Process on Linux - #137497
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]>
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.
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.
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.
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.
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.
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.
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
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.
Split out of #136630.
Summary
Creating the
ProcessInfothat backsProcess.WorkingSet64,Process.VirtualMemorySize64,Process.HandleCount,Process.StartTimeetc. on Linux reads/proc/<pid>/task/<tid>/statfor every thread of the process, even though that information is only needed whenProcess.Threadsis used. The cost (and allocations) scale with the number of threads, and are also paid for every process byProcess.GetProcesses().This changes it so the thread information is only read when
Process.Threadsis used.ProcessInforecords whether its threads have been read (ThreadsPopulated) so that other platforms, which populate the information up front, are unaffected.ProcPid) that was used to create theProcessInfo, so it still works when the pid namespace of/procdoes not match that of the process.Threadsis 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
Threadsis first accessed (and still reset byRefresh()). If the process has exited by then,Threadsis empty rather than containing the threads that existed when other information was first read.Tests
Adds Linux tests covering
Threadsbeing 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).