Skip to content

Yield between capped thread-pool semaphore spins on Linux - #138155

Draft
akmurray wants to merge 1 commit into
dotnet:mainfrom
akmurray:codex/linux-socket-dispatch
Draft

akmurray wants to merge 1 commit into
dotnet:mainfrom
akmurray:codex/linux-socket-dispatch

Conversation

@akmurray

Copy link
Copy Markdown

On a Linux x64 server handling 32 outstanding requests, an idle portable thread-pool worker can spend the entire semaphore spin budget polling before it parks. This patch gives other runnable threads a scheduling opportunity between alternating capped spins. It checks for a signal before yielding and only yields while there is budget left. The one-shot acquire, initial short backoffs, waiter registration and wake accounting keep their existing behavior. The single-processor path skips the spin loop.

Where the regression appeared

With the same application, Kestrel binaries and load process, #122726 took WebSocket throughput from 79,919 to 58,636 requests/s and HTTP from 103,495 to 79,238. These are comparisons between the commit and its immediate parent. Restoring only the parent's wait loop and iteration count on top of that commit recovered WebSocket throughput from 53,580 to 83,978 requests/s in a separate paired run. An earlier, smaller WebSocket drop was also reproducible at #117788. The report describes the component builds and their limitations. Historical source comparisons

Proposed patch on matched source builds

Runtime Transport Requests/s before → after Change Server CPU µs/request Managed B/request Client p99 ms
11.0.0 WebSocket 63,952 → 79,814 +24.8% 33.64 → 35.23 12,890 → 12,877 1.95 → 1.94
11.0.0 HTTP 83,726 → 95,605 +14.2% 30.06 → 29.48 12,277 → 12,265 1.65 → 1.66
12 alpha WebSocket 64,096 → 80,443 +25.5% 33.83 → 34.94 12,865 → 12,877 2.04 → 1.38
12 alpha HTTP 82,750 → 100,414 +21.3% 30.08 → 27.98 12,273 → 12,288 1.71 → 1.65

Each comparison used two independently restarted service processes per arm in A/B/B/A order. The 11.0.0 table uses four timed observations per arm per transport. The 12 alpha table uses two. Across these 24 timed observations, the driver completed 19,451,391 requests with zero errors, timeouts, reconnects or sampled reply-validation failures. The tested nightly was 12.0.0-alpha.1.26573.102, runtime source 4ab5617c35e2. The patch was compiled on upstream main as well.

The host was Docker Desktop on WSL2. The service used four guest logical CPUs and the client used two, with one request outstanding per connection. The application runs used a minimum of 256 workers and disabled dynamic GC adaptation. The standalone Kestrel echo app also reproduces the stock release difference using framework defaults, from 180,603 WebSocket round trips/s on 10.0.12 to 123,441 on 11.0.0. Client CPU placement affects the magnitude.

In separate 11.0.0 traces, the mean from epoll readiness to entering the successful receive fell from 35.2 to 24.0 µs on WebSocket and from 84.5 to 71.3 µs on HTTP. The WebSocket median rose from 4.7 to 7.5 µs while its p99 fell from 224 to 186 µs. The syscall itself averaged roughly 1 µs. Each trace covers all 32 connections and at least 126,000 matched cycles.

On the upstream nightly using framework-default thread-pool and GC settings, the public Minimal API JSON benchmark gained 43.4% at 32 clients and 35.1% at 256. Raw Kestrel JSON gained 45.8% and 36.2%, respectively. Client p99 decreased in all four comparisons. These are local aspnet/Benchmarks application runs using the fixed client, on the same WSL2 machine.

A separate, non-socket producer-to-worker control reduced single-item queue-to-start p99 from 23.47 to 7.14 µs. With two additional CPU-bound competitors, it fell from 24.30 to 4.61 µs. Batches of 32 increased from 1.84 to 2.97 million items/s. The report includes the control, CPU time and individual observations. Full benchmark results

The standalone WebSocket echo also improved with the proposed patch on .NET 11.0.0, from 101,209 to 143,317 round trips/s (+41.6%) with the application's worker and GC settings. The upstream System.Threading.ThreadPool.Tests suite ran on the patched Linux x64 nightly using the repository's test selection. It passed 68 tests and skipped two.

Extract the evidence ZIP and run cd repro && bash run.sh for stock releases. Use bash build-edge.sh release-fix "$HOME/socket-runtime-fix", then the emitted A/B command for the patch. The same script can rebuild the January parent/child comparison and the wait-loop reversal. I would appreciate a cross-check on native Linux with a separate load host.

Download the reproducer and measurements · Reproducer instructions

Prepared with assistance from OpenAI Codex.

Allow a runnable producer to post a signal while a worker is waiting for new work. Yield on alternating capped backoffs after an unsuccessful acquire, while there is still spin budget left.
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Nov 23, 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: @JulieLeeMSFT, @VSadov
See info in area-owners.md if you want to be subscribed.

@akmurray

Copy link
Copy Markdown
Author

@dotnet-policy-service agree company="Furth Fortune"

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.Threading 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.

1 participant