Repository navigation
Description
Description
When the cpuset of a running .NET process changes, the threads the runtime started (finalizer, tiered compilation, debugger, ...) keep the CPUs they had when they were created. The main thread, and other processes in the same cgroup, get the new set.
Processes started from those threads inherit the old set too. I ran into this with GitHub Actions self-hosted runners in Incus containers: Incus moves containers to other CPUs whenever another container starts or stops, and the job steps that Runner.Worker (.NET 8.0.31) starts ended up on 1-3 of the container's 8 CPUs, with nproc printing 1.
Reproduction Steps
As root on a systemd host with cgroup v2 (CPUs 4-9 need to exist):
dotnet new console -o app && cd app
echo 'Thread.Sleep(Timeout.Infinite);' > Program.cs
dotnet build -c Release -o out
systemd-run --scope --unit=affinity-repro -p AllowedCPUs=4-5 \
sh -c 'sleep infinity & exec dotnet out/app.dll' &
sleep 2
systemctl set-property --runtime affinity-repro.scope AllowedCPUs=4-9
pid=$(pgrep -f out/app.dll)
for t in /proc/$pid/task/*; do echo "$(cat $t/comm): $(grep Cpus_allowed_list $t/status | cut -f2)"; done
echo "sleep: $(grep Cpus_allowed_list /proc/$(pgrep -f 'sleep infinity')/status | cut -f2)"Expected behavior
Every thread ends up on 4-9, the same as sleep.
Actual behavior
dotnet: 4-9
dotnet-ust: 4-9
dotnet-ust: 4-9
.NET SynchManag: 4-5
.NET EventPipe: 4-5
.NET DebugPipe: 4-5
.NET Debugger: 4-5
.NET Finalizer: 4-5
.NET Tiered Com: 4-5
sleep: 4-9
Regression?
Not a .NET regression as far as I can tell. It shows up with Linux 6.2 and later (see below).
Known Workarounds
Don't change the cpuset of a running .NET process. For the Incus case I switched from limits.cpu (a CPU count, which Incus pins and moves around) to limits.cpu.allowance.
Configuration
.NET 10.0.12 (SDK 10.0.112, Ubuntu 24.04 packages) and .NET 8.0.31 (actions/runner 2.338.0), x64. Linux 7.2.10 on an Arch host; I ran the repro in an Ubuntu 24.04 Incus container. It only needs a systemd scope, so I'd expect the same on a plain host.
Other information
The runtime calls sched_setaffinity(0, ...) with the process's mask on every thread it starts (CPalThread::ThreadEntry in src/coreclr/pal/src/thread/thread.cpp). Since Linux 6.2 (torvalds/linux@8f9ea86fdf99) the kernel keeps a mask set that way across cpuset changes, so each thread stays on whatever the cpuset was when it started.
Activity
dotnet-policy-service commentedon Oct 18, 2026
Tagging subscribers to this area: @JulieLeeMSFT, @VSadov
See info in area-owners.md if you want to be subscribed.
SamJUK commentedon Nov 26, 2026
We hit this too, with the same setup: GitHub Actions runners in Incus system containers with
limits.cpu: "4"(Incus 6.0.5, Linux 7.0).Incus moved the container's cpuset four times in the first 90 seconds of one job as other runners started and stopped. A Playwright step started by Runner.Worker ended up with Chromium on
Cpus_allowed_list: 2while the container had four CPUs. Resetting the step's affinity withtaskset -apc 0-63 $$before starting Playwright took the same E2E suite from 7.0-7.6 minutes to 3.4 minutes.Skipping the
sched_setaffinitycall when the new thread's mask already matches the process mask would avoid storing a requested mask in the common case, so threads would follow later cpuset changes.Only reset a new thread's affinity when it differs from the process m…