Repository navigation
Client: completed requests no longer keep their responses alive - #4870
Conversation
There was a problem hiding this comment.
🟢 Approval recommended
The focused changes preserve response handling, follow existing pooling patterns, and have no unresolved findings.
No open issues 🎉
What changed in this PR
Reduces client response retention after completed requests, extending the pooled state-machine approach from #4849.
Changes:
- Clears the channel operation’s response reference after reading it.
- Uses pooled state machines for generated client async methods on .NET 6+, documenting single-await usage.
| File | Description |
|---|---|
| tools/Opc.Ua.SourceGeneration.Core/Generators/ClientApiTemplates.cs | Enables pooled async builders and documents task consumption. |
| src/Opc.Ua.Core/Stack/Tcp/UaSCBinaryClientChannel.cs | Releases the completed operation’s response reference. |
Review effort: Balanced
A request that waits long enough to be promoted (a Publish waits for the publishing interval) kept its response reachable from two dead old objects: the channel's WriteOperation (MessageBody) and the Task box of the generated SessionClient.*Async method (result and hoisted locals). Every gen0 and gen1 collection then kept the whole response through the card table until a full collection, so publish responses were promoted to gen2. - UaSCUaBinaryClientChannel.SendRequestAsync clears MessageBody once read. - Generated SessionClient async methods use PoolingAsyncValueTaskMethodBuilder on .NET 6+: a completed box drops its result and state machine. 100k monitored items: young data kept only by dead old objects 1073 KB -> 0. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Code coverage✅ Coverage gate passed.
Coverage is above the recorded baseline - consider ratcheting Thresholds live in |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #4870 +/- ##
=======================================
Coverage 84.31% 84.31%
=======================================
Files 3007 3007
Lines 444062 444075 +13
Branches 76909 76909
=======================================
+ Hits 374399 374433 +34
+ Misses 44688 44676 -12
+ Partials 24975 24966 -9
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
Problem
A client request that lives across a GC is promoted. Publish requests always are: they wait at the server for the publishing interval. When the response arrives, two dead old objects still reference it:
UaSCUaBinaryChannel.WriteOperation.MessageBody:UaSCUaBinaryClientChannel.SendRequestAsyncread it but never cleared it.SessionClient.*Asyncmethod. It isasync ValueTask<T>with the default builder, so a call that completes asynchronously leaves aTask<T>box holding the result and the hoistedresponselocals.The GC treats old objects as live. Every gen0 and gen1 collection therefore keeps the whole response alive through the card table until a full collection. With 100k monitored items, every
PublishResponse(100MonitoredItemNotifications) ended up promoted to gen2.Evidence (ClrMD scan of a live client dump, 25 sessions × 40 subscriptions × 100 items): 1,073 KB of gen0 was kept only by dead old objects, against 136 KB kept by live ones. The dead holders were exactly the two above, holding the same responses. Gen2 held 37 MB of dead notifications.
Change
SendRequestAsyncclearsoperation.MessageBodyonce it has read the response.ClientApiTemplates: generatedSessionClientasync methods usePoolingAsyncValueTaskMethodBuilderon .NET 6+. A completed box resets its result and state machine. This is the same pattern Client: fewer allocations per request (shared timeout timer, pooled state machines, fewer buffer collections) #4849 applied to the channel andManagedSession.Contract note: the generated
SessionClientcalls now return pooled value tasks, so each must be awaited once, like any value task. All sync-over-async call sites in the repo use.AsTask()first.After the fix, young data kept only by dead old objects is 0 KB (was 1,073 KB).
Measurements
perf/ServerLoadHarness, clientclient-gc:line. Master vs this branch, 3 alternating rounds, runs gated on foreign CPU load (shared machine). Ranges are over the rounds.The first fix round at 100k ran while another process starved the server (46k notif/s); it is left out of the fix row.
gc): card marking 3.3 → 2.55 ms (2.8 → 0.72 MB marked), plan+relocate+compact 6.05 → 3.27 ms.Taskbox per call is gone.Tests
net10.0: Opc.Ua.Client.Tests 3283 passed / 0 failed, Opc.Ua.Core.Tests 6535/0, Opc.Ua.SourceGeneration.Core.Tests 4412/0, Opc.Ua.Sessions.Tests 857/0. The net48 Release build of Opc.Ua.Client (with Core) is clean.
🤖 Generated with Claude Code