Skip to content

harness: cap concurrent re-attach streams, so attach readers cannot exhaust Redis connections (follow-up to #471) #478

Open

Description

@pdettori

Follow-up from #471 (detachable turns). The final branch review deferred this by ruling.

Goal

One client, or a few, cannot exhaust the harness's Redis connections by opening re-attach streams.

Current state

  • TurnRegistry.attach() (harness/src/turn-registry.ts) gives every GET /v1/turn?sessionId= its own duplicated Redis connection (client.duplicate()). This is necessary because XREAD BLOCK would otherwise stall every other command on the shared client.
  • The connection is released when the stream ends, the client disconnects, or close() runs. Nothing caps how many are open at once. There is no limit per process, per session, or per subject.
  • An authenticated client holding a valid session token can open attach streams in a loop. Each one holds a Redis connection until it ends, so the client can push Redis toward maxclients and starve everything else that shares it: sessions, leases, the control plane.
  • mocactl itself opens one attach per resume, and re-attaches at most 5 times after a dropped stream. Normal use is far below any sensible cap.

Scope

  1. Cap concurrent attach readers per process, for example SH_TURN_ATTACH_MAX with a default sized against the worker's Redis budget. Past the cap, answer 503 {"error":"attach_unavailable"} with Retry-After. mocactl already retries on 503s that carry Retry-After.
  2. Cap concurrent attaches per session at a small number (2–4), so one session cannot use the whole process budget. A further attach for that session gets 429 or 409; pick one and document it.
  3. Optional: serve several attaches of the same turn from one reader in the process (fan-out). That would make the per-session cap mostly moot.
  4. Expose the current attach-reader count in the worker's advisory stats message, so /metrics can show it.

Acceptance

  • With the cap at N, the (N+1)th concurrent attach in a process gets the documented status, and it recovers once one ends.
  • The per-session cap holds across attaches to the same session.
  • A test shows reader connections return to zero after streams end, abort, or close().
  • deploy/compose README, vm env example, and k8s base document the new settings.

Activity

  1. added
    enhancement
    on Oct 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions