Repository navigation
Only reset a new thread's affinity when it differs from the process mask - #136027
Merged
Merged
Conversation
|
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. |
Contributor
Author
|
@dotnet-policy-service agree |
Contributor
|
Tagging subscribers to this area: @JulieLeeMSFT, @VSadov |
jkotas
reviewed
Oct 18, 2026
Contributor
|
Tagging subscribers to this area: @anicka-net, @dotnet/gc |
Hekzory
requested review from
a team,
BrzVlad,
MichalStrehovsky,
akoeplinger and
jeffhandley
as code owners
November 26, 2026 19:44
Hekzory
force-pushed
the
pal-affinity-cpuset
branch
from
November 26, 2026 19:48
fcdebbb to
44e38ce
Compare
Hekzory
force-pushed
the
pal-affinity-cpuset
branch
from
November 26, 2026 19:50
44e38ce to
cc0e9f4
Compare
jkotas
reviewed
Nov 26, 2026
3 tasks
jkotas
approved these changes
Nov 27, 2026
Member
|
/ba-g dead letter |
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.
Fixes #136015
On Linux,
CPalThread::ThreadEntrycallssched_setaffinityon 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 CPUs4-5to5-6, a runtime thread can end up on CPU5alone. 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::ThreadEntryand 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. IfProcess.ProcessorAffinitynarrows the main thread to CPU5and a thread on4-5then starts another thread, the new thread inherits4-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
4-5to5-6. An existing managed thread, a thread it started after the move, and a child process all got5-6. I also checked expansion to4-9and a move to the disjoint set8-9.4started another thread, which inherited CPU4. In theProcess.ProcessorAffinitycase above, the new thread inherited4-5.4-7, the four heap threads each kept one CPU and all four background GC threads got4-7in both CoreCLR and Native AOT. Narrowing the main thread to CPU5before the first background collection gave all four background threads CPU5. 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, andProcessTests.TestProcessorAffinitypassed on Linux x64 (124 passed, 11 skipped).