Skip to content

server: take the registry write lock off the dispatch path - #211

Merged
shawnburke merged 3 commits into
mainfrom
shawn/tunnel-lock-granularity
Dec 3, 2026
Merged

shawnburke merged 3 commits into
mainfrom
shawn/tunnel-lock-granularity

Conversation

@shawnburke

@shawnburke shawnburke commented Dec 2, 2026 •

Copy link
Copy Markdown
Collaborator

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.

image

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 the GetIdentity reads 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:

  • the periodic re-registration sweep (18 calls/hr, ~22 ms each);
  • all-busy acquire polling (40/hr);
  • CPU throttling (none; pods at ~0.16 of 1.5 cores).

It is the volume of exclusive acquisitions on one lock.

What

  • AcquireIdleStream and the release callback hold the registry lock for reading only, enough to keep the entry and its Streams map stable.
  • The idle stack moves out of clientEntry into its own idleStack type (server/tunnel/idle_stack.go), which owns the slice and a per-token mutex. Nothing else touches the slice. pop takes a callback that checks whether a handle is still the one registered for its stream ID.
  • Register/Unregister keep the write lock.
  • Popped and removed idle slots are cleared so the backing array doesn't keep closed streams alive.
  • make -C server test now runs with -race. The idle stack 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.

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:

  • lock order is always registry read lock → idleMu;
  • there is no recursive read lock;
  • stale handles are rejected by pointer comparison;
  • the fallback scan is correct under the read lock alone.

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

  • TestAcquireAndReleaseNeedOnlyTheReadLock holds the read lock and checks that acquire and release still complete. It fails on main.
  • TestConcurrentAcquireReleaseWithChurn runs 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 -race if idleMu is removed.
  • idle_stack_test.go adds direct unit tests for LIFO order, stale-entry skipping, remove, and cleared slots.
  • make -C server test (with -race) passes.
  • TestDispatch_RequestBodyFailureHoldsStreamUntilAgentReports is flaky under -race: it fails about 1 run in 30 on main as well, so this PR didn't introduce it.

🤖 Generated with Claude Code

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
shawnburke force-pushed the shawn/tunnel-lock-granularity branch from cc12b88 to 032837b Compare December 3, 2026 01:33
@shawnburke shawnburke changed the title server: per-token locks and direct stream handoff instead of one registry mutex server: take the registry write lock off the dispatch path Dec 3, 2026
shawnburke and others added 2 commits December 3, 2026 12:41
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
shawnburke marked this pull request as ready for review December 3, 2026 01:44
@shawnburke
shawnburke requested review from a team and keithfz December 3, 2026 01:44
@shawnburke shawnburke added the bypass-trivy-fail Allow trivy PR scan to fail for unrelated issues. label Dec 3, 2026
@shawnburke
shawnburke enabled auto-merge (squash) December 3, 2026 02:20
@shawnburke
shawnburke merged commit 25399a7 into main Dec 3, 2026
31 of 33 checks passed
@shawnburke
shawnburke deleted the shawn/tunnel-lock-granularity branch December 3, 2026 16:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bypass-trivy-fail Allow trivy PR scan to fail for unrelated issues.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants