Fix concurrency leak during enforceContextLimit and during stall retry#445
Open
sirn wants to merge 2 commits into
Open
Fix concurrency leak during enforceContextLimit and during stall retry#445sirn wants to merge 2 commits into
sirn wants to merge 2 commits into
Conversation
Pre-dispatch context limit enforcement now runs before the concurrency slot is acquired. Previously, if enforceContextLimit threw a ContextLengthExceededError, the acquired slot was never released — leaking exactly one slot per oversized request until the provider deadlocked at maxConcurrency.
When the TTFB stall detection aborts the fetch request, the catch block either continues the failover loop or throws the stall error without calling doRelease(). This leaks the concurrency slot permanently. Each stalled request increments the count by one, eventually deadlocking the provider at maxConcurrency.
mcowger
approved these changes
May 20, 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.
This PR fixes two concurrency leaks.
enforceContextLimit leaks concurrency slot
When "Enforce Limits" is on for an alias, an API call that hit that limit will cause concurrently to be permanently leaked.
Steps to reproduce
enforce_limit: truewith a smallcontext_length(e.g. 100).context_lengthcontext_length_exceededand concurrency permanently increased by 1Root cause
In
dispatcher.ts,acquire()was called before context length check (enforceContextLimit). When the check fails, it throwsContextLengthExceededErrorand never fall through to the path wheredoReleaseis called. This caused the concurrency count to get permanently leaked.Fix
Move
enforceContextLimitbeforeacquire()so context length.TTFB stall timeout leaks concurrency slot
When TTFB stall timeout is configured with multiple targets, and both of them happen to fail, the first provider's concurrency count will be permanently leaked.
Steps to reproduce
"stream": true.Root cause
After TTFB stall detection aborts the fetch and failover is configured (
canRetryStallistrue), it skipsthrowand callscontinue. SincedoRelease()is only done in the outercatchblock, this means thatdoRelease()was never gets called except for the last provider. This caused the concurrency count to get permanently leaked.Fix
Add
doRelease()in the inner catch block too.