Skip to content

Only reset a new thread's affinity when it differs from the process mask - #136027

Merged
jkotas merged 2 commits into
dotnet:mainfrom
Hekzory:pal-affinity-cpuset
Nov 27, 2026
Merged

jkotas merged 2 commits into
dotnet:mainfrom
Hekzory:pal-affinity-cpuset

Conversation

@Hekzory

@Hekzory Hekzory commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #136015

On Linux, CPalThread::ThreadEntry calls sched_setaffinity on each new thread to give it the main thread’s CPU mask. Since Linux 6.2 (torvalds/linux@8f9ea86fdf99), the kernel remembers that explicit mask across cpuset changes. When a container moves from CPUs 4-5 to 5-6, a runtime thread can end up on CPU 5 alone. A process started from that thread inherits the same restriction.

The first version of this PR skipped the affinity reset when a new thread already had the main thread’s mask. Following the review discussion, this version removes the reset from CPalThread::ThreadEntry and keeps it only for the background server GC threads that need it. Ordinary threads inherit their creator’s affinity. This changes one observable case on CoreCLR. If Process.ProcessorAffinity narrows the main thread to CPU 5 and a thread on 4-5 then starts another thread, the new thread inherits 4-5.

The reset was originally added for background server GC threads (dotnet/coreclr#24801). These can all be started by a server GC thread pinned to one CPU. With server GC affinitization enabled, each background GC thread now reads the main thread’s current mask and applies it when it starts. CoreCLR and Native AOT share this code.

Testing

  • On Linux 6.18, I moved a running Docker container from CPUs 4-5 to 5-6. An existing managed thread, a thread it started after the move, and a child process all got 5-6. I also checked expansion to 4-9 and a move to the disjoint set 8-9.
  • A managed thread pinned to CPU 4 started another thread, which inherited CPU 4. In the Process.ProcessorAffinity case above, the new thread inherited 4-5.
  • With server GC on CPUs 4-7, the four heap threads each kept one CPU and all four background GC threads got 4-7 in both CoreCLR and Native AOT. Narrowing the main thread to CPU 5 before the first background collection gave all four background threads CPU 5. For the Native AOT execution test, I applied the same GC change to the runtime sources matching the test SDK.
  • System.Threading.Thread.Tests, System.Threading.ThreadPool.Tests, and ProcessTests.TestProcessorAffinity passed on Linux x64 (124 passed, 11 skipped).

@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Oct 18, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
13 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@Hekzory

Hekzory commented Oct 18, 2026

Copy link
Copy Markdown
Contributor Author

@dotnet-policy-service agree

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @JulieLeeMSFT, @VSadov
See info in area-owners.md if you want to be subscribed.

Comment thread src/coreclr/pal/src/thread/thread.cpp Resolved
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @anicka-net, @dotnet/gc
See info in area-owners.md if you want to be subscribed.

@dotnet-policy-service dotnet-policy-service Bot added the linkable-framework Issues associated with delivering a linker friendly framework label Nov 26, 2026
@Hekzory
Hekzory force-pushed the pal-affinity-cpuset branch from fcdebbb to 44e38ce Compare November 26, 2026 19:48
@Hekzory
Hekzory force-pushed the pal-affinity-cpuset branch from 44e38ce to cc0e9f4 Compare November 26, 2026 19:50
Comment thread src/coreclr/gc/env/gcenv.os.h Resolved

@jkotas jkotas left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM. @janvorli ?

@janvorli janvorli left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM, thank you!

@jkotas

jkotas commented Nov 27, 2026

Copy link
Copy Markdown
Member

/ba-g dead letter

@jkotas
jkotas merged commit 5aded98 into dotnet:main Nov 27, 2026
100 of 102 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Dec 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-GC-coreclr community-contribution Indicates that the PR has been added by a community member linkable-framework Issues associated with delivering a linker friendly framework

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Runtime-created threads don't follow cgroup cpuset changes on Linux 6.2+

3 participants