Repository navigation
net11.0: explain the higher CPU per request (thread-pool spinning), start the client writer inline - #5078
Merged
Conversation
…read-pool spinning On .NET 11 the server and client use 15-85 % more CPU per request than on .NET 10 with identical allocations and thread-pool work items. The extra time is user-mode spinning of thread-pool workers: the .NET 11 pool spins idle workers for 1024 backoff units (UnfairSemaphoreSpinLimit, 70 iterations on .NET 10), also after a wake-up that found no work, unless more than two thirds of the processors are busy (SpuriousDispatchNoSpinThreshold). The client queued its channel writer with preferLocal: the requesting thread ran the item itself when it awaited the response, but the enqueue also woke a pool worker that found nothing and spun. SendRequestAsync now starts the writer on its own thread once it released the channel gate. Client work items per request drop from 3 to 2; client CPU per request on .NET 11 -6 to -19 %, on .NET 10 0 to -13 %, throughput unchanged or +2-3 %. ServerScalability.md documents the runtime settings that restore the .NET 10 CPU cost; the load harness reports user-mode CPU and thread-pool work items. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Contributor
Author
|
@marcschier Your take on this? |
marcschier
approved these changes
Nov 13, 2026
Contributor
Author
|
@marcschier agree with opening an issue against dotnet/runtime? |
Code coverage✅ Coverage gate passed.
Uncovered changed lines
Coverage is above the recorded baseline - consider ratcheting Thresholds live in |
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## master #5078 +/- ##
=======================================
Coverage 83.59% 83.60%
=======================================
Files 3780 3780
Lines 553499 553520 +21
Branches 94437 94442 +5
=======================================
+ Hits 462723 462765 +42
+ Misses 59563 59555 -8
+ Partials 31213 31200 -13
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
romanett
added a commit
that referenced
this pull request
Nov 14, 2026
… waits (.NET 11 spurious wake-ups) (#5084) Follow-up to #5078 (.NET 11 thread-pool spinning after spurious wake-ups, dotnet/runtime#137494). ## Change A request waits for its response on a `ManualResetValueTaskSourceCore` with `RunContinuationsAsynchronously` (the receive loop completes it under the channel gate). The BCL queues that continuation with `preferLocal: true`, so either the receive loop's thread runs it once its next receive goes pending, or a worker that the enqueue woke takes it. When the loop's thread runs it, the woken worker found no work, and on .NET 11 it spins up to 1024 backoff units before it blocks. The receive loop now defers the completion of a response (`ResponseContinuationDeferral`, a thread-static slot filled in a scope around the synchronous part of `OnChunkReceivedAsync`) and completes it on its own thread when one of its waits (write capacity, next receive, chunk dispatch) goes pending: - The loop's awaits use an awaiter that **first registers the loop's continuation** on the pending wait and **then** runs the deferred continuation inline. The loop no longer depends on the thread at that point, so a continuation that blocks (for example a synchronous call on the same channel) cannot stall the receive loop; this is the same exposure as a pool thread today. `BlockingContinuationDoesNotStallTheLoopAsync` covers it (it fails when the two steps are swapped). - Never under the gate or a lock: completions run after `OnChunkReceivedAsync` returned. - A thread holds **at most one** deferred completion. A later response completes the held one on the pool, as before this change: a continuation run inline may block until another of its requests completes, and that one must not wait behind the blocked thread (`ContinuationBlockingOnTheNextResponseDoesNotDeadlockAsync`). - A held completion also goes to the pool once the loop dispatched 4 more chunks without its receive going pending, so a channel whose receives keep completing synchronously does not hold a response back. - A loop that ends completes the held one on the pool, in a `finally` of the loop method itself (it runs on the thread that dispatched the last chunk). - A loop that resumed inside another loop's dispatch on the same thread leaves the inline completion to the outer loop, which may hold locks. - Timeouts, cancellation and `Dispose` still complete the wait directly. Client thread-pool work items per request: 2 → 1. ## Measurements **After the review fixes** (one held completion; run 4, medians of 4 rotated rounds, vs #5078 tip): | Scenario | net11 client CPU/op | net11 throughput | net10 client CPU/op | net10 throughput | |---|---|---|---|---| | Read 1 value | 43.6 → 40.5 (−7 %) | +2 % | 38.7 → 35.7 (−8 %) | +3 % | | Write 100 | 175.1 → 140.4 (−20 %) | +1 % | 114.9 → 99.4 (−13 %) | +2 % | | Browse | 133.1 → 95.2 (−28 %) | +2 % | 79.2 → 70.7 (−11 %) | −2 % | | Read 100, encrypt | 113.8 → 109.5 (−4 %) | +1 % | 97.3 → 92.0 (−5 %) | +2 % | | Read 100, encrypt, 4 cores | 140.2 → 139.4 (−1 %) | 0 % | 113.6 → 105.1 (−7 %) | +1 % | Client work items per request 2.0 → 1.0 in every case. Earlier runs of the first version (up to 16 held completions) follow. Load harness, 24 logical processors, 20 sessions, 12 s + 6 s warm-up, medians of 4 rotated rounds per side, runs with more than 3 cores of foreign CPU discarded. Baseline = #5078 branch tip (c1d9d6b). Client CPU in µs per operation, `usr` = user-mode µs/op, `wi` = thread-pool work items/op. "4 cores" = `--client-cores 20` (server on the remaining 4 processors, encrypted). **.NET 11** (run 2 / confirming run 3): | Scenario | Client CPU/op | Client usr/op | Client wi/op | Throughput | Server CPU/op | |---|---|---|---|---|---| | Read 1 value | 42.4 → 40.3 (−5 %) / 44.2 → 40.8 (−8 %) | −7 % / −10 % | 2.0 → 1.0 | +1 % / +2 % | +1 % / +3 % | | Write 100 | 159.8 → 130.9 (−18 %) | −20 % | 2.0 → 1.0 | 0 % | 0 % | | Browse | 131.3 → 99.8 (−24 %) / 129.6 → 99.3 (−23 %) | −26 % / −27 % | 2.0 → 1.0 | +8 % / +2 % | +1 % / −3 % | | Read 100, encrypt | 115.2 → 108.2 (−6 %) / 123.6 → 111.6 (−10 %) | −10 % / −6 % | 2.0 → 1.0 | −2 % / +1 % | +4 % / −4 % | | Read 100, encrypt, 4 cores | 141.5 → 139.3 (−2 %) / 148.2 → 136.4 (−8 %) | +18 % / −7 % | 2.0 → 1.0 | +1 % / +2 % | −2 % / −2 % | **.NET 10** (run 2): | Scenario | Client CPU/op | Client wi/op | Throughput | |---|---|---|---| | Read 1 value | 37.6 → 34.9 (−7 %) | 2.0 → 1.0 | +1 % | | Write 100 | 95.3 → 91.4 (−4 %) | 2.0 → 1.0 | +2 % | | Browse | 76.9 → 68.1 (−11 %) | 2.0 → 1.0 | +1 % | | Read 100, encrypt | 101.7 → 98.9 (−3 %) | 2.0 → 1.0 | −1 % | | Read 100, encrypt, 4 cores | 103.2 → 99.2 (−4 %) | 2.0 → 1.0 | −1 % | The encrypted scenario moves ±5 % between identical builds (A/A spread in these runs), so −2 %/−1 % there is noise; the confirming .NET 11 run was +1 %. ## Dropped: server writer started at the worker's park point The second follow-up of #5078 was implemented and measured, and is **not** included. `BeginWriteSymmetricMessage` deferred `ScheduleWriter()` while a request worker ran a request synchronously; the worker started the deferred writers inline when it found no request (before the fair queue spins), when a request went asynchronous, when it handed back its permit (per-request dispatch), or after 1–2 more requests, and only on threads the pool dispatched it on. Server work items per request fell by 32–53 %, but .NET 11 server CPU per request did not follow: | .NET 11, vs #5078 | Read 1 | Browse | Read 100 encrypt | Read 100 encrypt, 4 cores | Write 100 | |---|---|---|---|---|---| | Server CPU/op, bound 2 (run 1 / run 2) | −8 % / −8 % | +11 % / +7 % | −2 % / −3 % | 0 % | 0 % / +3 % | | Throughput, bound 2 | −5 % / −5 % | +7 % / +4 % | −2 % / −5 % | +1 % / +3 % | 0 % / −2 % | | Server CPU/op / throughput, bound 1 | −7 % / −2 % | 0 % / +1 % | −1 % / −1 % | +1 % / +1 % (p99 +73 %) | – | | Server CPU/op / throughput, park only | −3 % / −5 % | +3 % / −2 % | 0 % / −1 % | 0 % / +1 % | – | It also raised client CPU per Read by 11–30 % (responses leave later and in bursts). Write kept 4 work items per request: its responses are sent after the handler went asynchronous. So the queued writer is not where the server's .NET 11 spin cost is, and deferring it costs latency and closed-loop throughput. ## Tests - New `ResponseContinuationDeferralTests` (deferral in and out of scope, inline completion, completion on loop exit, one held completion, the 4-chunk bound, blocking continuation vs. loop, continuation blocking on the next response). - After the review fixes: Opc.Ua.Core.Tests (Stack.*) 3411 and Opc.Ua.Client.Tests 3842 passed on net10.0 and net11.0. - `Opc.Ua.Core.Tests` (Stack.Transport/Server/Client/Tcp/Bindings): 3409 passed on net10.0 and net11.0. - `Opc.Ua.Client.Tests`: 3839 passed on net10.0 and net11.0. - `Opc.Ua.Server.Tests`: 11034 passed; 8 failures in `PhysicalFileSystemProviderConfinementTests` on both runtimes, all "insufficient privileges" creating symbolic links on the local machine, unrelated to this change. - net48 build of Opc.Ua.Core.Tests. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
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.
Why net11.0 costs more CPU per request than net10.0
The net10 → net11 table in #5072 showed equal allocations but +14–68 % CPU per request. The cause is the .NET 11 thread pool, not our code:
LowLevelLifoSemaphorelets an idle worker spin for 1024 units of exponential backoff before blocking (UnfairSemaphoreSpinLimit; .NET 10: 70 iterations). This comes from decompiling CoreLib 10.0.15 against 11.0.0.SpuriousDispatchNoSpinThreshold, 16 on 24 cores).ThreadPool.UnsafeQueueUserWorkItem(..., preferLocal: true): the issuing thread runs the item itself, but the enqueue still signals a second worker.Harness evidence (24 logical processors, 20 sessions, medians of 4 rotated rounds; runs with foreign CPU load discarded):
Same work, more spinning. Allocations and thread-pool work items per request are identical on net10 and net11. The extra CPU is all user mode; kernel time per request is unchanged.
The spin settings remove all of it. With
DOTNET_ThreadPool_SpuriousDispatchNoSpinThreshold=0on net11, compared with net11 defaults:UnfairSemaphoreSpinLimit=0x46gives the same CPU recovery but not the throughput gain.UnfairSemaphoreWakeCooldown=0has no effect.The new signalling path spins too. EventPipe samples show
LowLevelLifoSemaphore.MaybeWakeWaiterSlowat 22 % (client) and 6 % (server) of on-CPU samples on net11. That is the signalling side's CAS loop with backoff.Ruled out. Runtime-async: our assemblies have no runtime-async methods; only CoreLib and Sockets do. GC: counts and allocations are unchanged.
Changes
SendRequestAsyncnow starts the channel writer on its own thread once it has released the gate (BeginWriteMessageDeferred+StartWriterInline). Before, it queued the writer withpreferLocal: true. The writer still never runs under the gate orSendOrder. The connect-replay path still schedules the writer on the pool. The server writer is unchanged (A1 measured the inline start there as −30 % encrypted on 4 cores).server:andclient-process:now reportuserMsandworkItems(ThreadPool.CompletedWorkItemCount).docs/ServerScalability.mdgets a new section, Thread-pool spinning on .NET 11, with the two runtime settings and their measured effect.Paired A/B, this change against the #5072 tip (same harness)
Client CPU µs per request, median of 4 rounds:
Client work items per request go from 3.0 to 2.0. Server CPU is unchanged within noise. This change recovers about a quarter to a third of the client regression; the runtime settings recover the rest.
Tests
Opc.Ua.Core.TestsStack.Transport/Client/Tcp (net10.0): 1295 passed.Opc.Ua.Client.Tests(net11.0): 3839 passed, 2 skipped.Opc.Ua.Corebuilds for all TFMs with 0 warnings.Upstream
Reported as dotnet/runtime#137494: on .NET 11.0.0 (Windows x64, 24 logical processors), request/response traffic over loopback with ~20 connections uses 40–85 % more CPU per request than on .NET 10. The cause is the full-budget spin after spurious dispatch when fewer than ⅔ of the processors are busy. Setting
SpuriousDispatchNoSpinThreshold=0removes the extra CPU and raises throughput. That suggests a lower default threshold, or a spin budget that adapts to the recent spurious-wake rate. TheMaybeWakeWaiterSlowbackoff on the signalling path is also worth a look.Follow-ups (not in this PR)
ManualResetValueTaskSourceCorewithRunContinuationsAsynchronously, queuedpreferLocalfrom the receive loop) causes the same spurious wake-up. The receive loop could run completed continuations inline when its next socket receive goes pending.preferLocalitem ran anyway, instead of queuing it.🤖 Generated with Claude Code