Repository navigation
server: take the registry write lock off the dispatch path - #211
Merged
Merged
Conversation
Every dispatched call acquires a stream and later releases it, and both took the ClientRegistry's single write lock. So every call on the server, for every token, queued on one mutex alongside stream register/unregister and the several GetIdentity reads each request makes. The production mutex profile charges almost all lock wait to the release path's Unlock. Acquire and release now hold the registry lock only for reading, enough to keep the entry and its Streams map stable, and serialize on a per-token idleMu for the idle stack itself. Register and Unregister keep the write lock, which excludes every idleMu holder. Popped and removed idle slots are now cleared so the backing array does not keep closed streams alive. Server tests now run with -race: idle is guarded by two different locks, and a missing idleMu is otherwise invisible -- TryAcquire's CAS keeps a stream from being handed out twice either way. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
shawnburke
force-pushed
the
shawn/tunnel-lock-granularity
branch
from
December 3, 2026 01:33
cc12b88 to
032837b
Compare
pushIdle, popIdle and removeIdle now take idleMu themselves instead of leaving it to callers. Register and Unregister used to skip it on the grounds that the write lock already excludes every other idle user; the uncontended lock costs nothing there, and the rule becomes simply that idle is only touched through these three methods. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The stack, its lock, and push/pop/remove were clientEntry methods and fields, but none of it is registry logic. idleStack owns the slice and its mutex; the registry passes pop a check for whether a handle is still the registered one for its stream ID, which is the only thing it needs from the entry. Adds direct unit tests for LIFO order, stale-entry skipping, remove, and cleared slots alongside the registry-level ones. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
shawnburke
marked this pull request as ready for review
December 3, 2026 01:44
shawnburke
enabled auto-merge (squash)
December 3, 2026 02:20
keithfz
approved these changes
Dec 3, 2026
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.
Context
Seeing some contention on the tunnel server b/c we are sharing a lock b/t registration and dispatch. Dispatch is fast path and should never be blocked by other concerns. This change consolidates the idle stream aquisition into its own helper which has its own lock.
Why
Every dispatched call acquires a tunnel stream and later releases it, and both took
ClientRegistry's single write lock. Every call on the server, for every token, queued on that one mutex alongside stream register/unregister and theGetIdentityreads each request makes.In a production mutex profile from a single-tenant dedicated environment (1h window), 99.8% of lock wait — about 11 s/min across 3 pods — is charged to the release path's
Unlock. Go charges wait to the unlock that wakes the waiter, not to the long holder, and nothing holds this lock long. Ruled out against the same window:It is the volume of exclusive acquisitions on one lock.
What
AcquireIdleStreamand the release callback hold the registry lock for reading only, enough to keep the entry and itsStreamsmap stable.clientEntryinto its ownidleStacktype (server/tunnel/idle_stack.go), which owns the slice and a per-token mutex. Nothing else touches the slice.poptakes a callback that checks whether a handle is still the one registered for its stream ID.Register/Unregisterkeep the write lock.make -C server testnow runs with-race. The idle stack is guarded by two different locks, and a missingidleMuis otherwise invisible:TryAcquire's CAS keeps a stream from being handed out twice either way.Effect: different tokens no longer contend on dispatch, and readers no longer queue behind acquire/release. On one hot token, acquire and release still serialize, now on a tiny per-token lock. Register/unregister churn still takes the global write lock; per-token registry locking is a possible follow-up if the profile still shows it.
Review notes
An adversarial concurrency review found no bug in the acquire/release locking:
idleMu;It did catch a bug in an earlier draft that also moved
ForEach's callback outside the lock. That could re-announce a token to BROKER_SERVER after its disconnect. The change was dropped from this PR.Testing
TestAcquireAndReleaseNeedOnlyTheReadLockholds the read lock and checks that acquire and release still complete. It fails onmain.TestConcurrentAcquireReleaseWithChurnruns 16 goroutines of acquire/release against stream register/unregister. It checks that no stream is lost or handed out twice and that the idle stack stays bounded. It reports data races under-raceifidleMuis removed.idle_stack_test.goadds direct unit tests for LIFO order, stale-entry skipping,remove, and cleared slots.make -C server test(with-race) passes.TestDispatch_RequestBodyFailureHoldsStreamUntilAgentReportsis flaky under-race: it fails about 1 run in 30 onmainas well, so this PR didn't introduce it.🤖 Generated with Claude Code