feat(interception): bound concurrent model requests per server - #2576
Closed
faresobeid wants to merge 1 commit into
Closed
feat(interception): bound concurrent model requests per server#2576faresobeid wants to merge 1 commit into
faresobeid wants to merge 1 commit into
Conversation
InterceptionServerConfig.max_concurrent_requests (None = unlimited) puts an asyncio semaphore around the upstream model request, shared by every session on the server: rendered calls hold a slot for get_response; streamed calls hold it from relay until the upstream connection closes. Tool execution, replay-cache hits and coalesced retries take no slot.
faresobeid
marked this pull request as ready for review
September 10, 2026 09:18
Contributor
ApprovabilityVerdict: Approved at Macroscope's review found this PR approvable — Adds a localized, opt-in per-server concurrency limit while preserving unlimited admission by default. Both rendered and streamed request paths are covered by integration tests, and unrelated routes remain unchanged. You can add or adjust custom eligibility rules. Learn more. |
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.
What
InterceptionServerConfig.max_concurrent_requests: PositiveInt | None = None. When set, anasyncio.Semaphorebounds the upstream model requests in flight across every session registered on that server:client.get_response(...)only — response interceptors /@stops run after release;client.relay(...)and release when the upstream connection closes (reply.close()), which now sits on oneAsyncExitStackwith the slot so the two always free together, on every exit path (afinallyin_streamcloses the stack beforerecord_call);/v1/modelsand aux routes take no slot;Nonekeeps today's behavior.The limit is per server: it applies to the
serverandstaticinterception shapes (staticlists oneInterceptionServerConfigper server). Theelasticpool mints servers with the default config and stays unlimited; a pool-wide knob is a separate change.The same concern was bundled into the closed #2552 with unrelated changes; this reimplements only the request limit on current
main.Why
Rollout concurrency does not bound inference load: a recursive harness (rho in the data-flywheel pipeline, ~90 concurrent rollouts) fans out several model requests per rollout, so the provider sees bursts well past the rollout cap and answers with 429s that then surface as rollout errors. A per-server admission limit puts the bound where every request already passes.
Validation
tests/v1/test_interception.py::test_max_concurrent_requests_bounds_upstream[rendered|streamed]: a realInterceptionServerwithmax_concurrent_requests=2, five sessions sharing a gated stubClientthat counts in-flight upstream requests (the streamed stub decrements only onclose()), five concurrentPOST /v1/chat/completionsover HTTP. Asserts exactly two reach upstream while the rest queue, peak in-flight stays 2, and all five complete with a committed turn. With the limit unset the same probe reaches peak 5.uv run pytest tests/v1 -m "not e2e": 88 passed.uv run ruff check,uv run pre-commit run --all-files: clean.Note
Bound concurrent upstream model requests in
InterceptionServermax_concurrent_requestsfield (PositiveInt) toInterceptionServerConfigin server.pysamplehandler and streamed relay handler to bound concurrent upstream model requestsGatedClienttest double and concurrency tests in test_interception.py to verify the limit is respected for both rendered and streamed pathsmax_concurrent_requestsset use a no-op context manager, preserving existing unlimited admissionMacroscope summarized 507e414.
Note
Medium Risk
Changes admission control on the central model proxy path; misconfigured limits could throttle or delay rollouts, but default
Nonepreserves current behavior.Overview
Adds optional
max_concurrent_requestsonInterceptionServerConfigso a single interception server can cap how many upstream model calls are in flight across all registered sessions (Nonekeeps unlimited admission).Non-streaming turns acquire a semaphore only around
client.get_response; streamed turns acquire beforeclient.relayand hold the slot until upstream teardown by pairing the semaphore withreply.close()on anAsyncExitStack(with idempotent cleanup on every exit path). Tool routes, replay-cache hits, and aux/model listing paths are unchanged and do not consume slots.New integration tests drive five concurrent
/v1/chat/completionscalls against a real server with limit2, using a gated stub client to assert queuing, peak in-flight of 2, and successful completion for both rendered and streamed modes.Reviewed by Cursor Bugbot for commit 507e414. Bugbot is set up for automated code reviews on this repo. Configure here.