Conversation
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.
Overview
This PR fixes a state-leak bug in
CircuitBreaker.executewhere theactiveTrialsentry for a breaker was only removed infinallywhenmetrics.statewas stillHALF_OPEN. Once a trial call closed or re-opened the breaker, the key was left behind, so the nextHALF_OPENwindow rejected every call with "Only one trial call allowed" — permanently wedging the breaker until restart.The fix tracks slot acquisition in a local flag and always releases the slot in
finallywhen this invocation actually acquired it. A regression test cycles the breaker through two fullOPEN → HALF_OPEN → CLOSEDrecoveries under fake timers.Related Issue
Changes
🔧 Circuit breaker fix
src/lib/circuitBreaker.tsexecute, introduce a localacquiredTrialSlotflag set only when this invocation successfully addsbreakerKeytoactiveTrialsin theHALF_OPENbranch.finally, deletebreakerKeyfromactiveTrialswheneveracquiredTrialSlotis true, regardless of the resultingmetrics.state(previously gated onstate === HALF_OPEN).activeTrials.🧪 Regression tests
src/lib/circuitBreaker.test.tsOPEN → HALF_OPEN → CLOSED → OPEN → HALF_OPEN → CLOSED) and asserts both cycles succeed.activeTrialsis empty after eachexecutecall completes (success, failure, and rejection paths).HALF_OPENstill allow exactly one trial and reject the rest.Verification Results
OPEN → HALF_OPEN → CLOSEDtwice under fake timersactiveTrialsis empty after anyexecutecall completesHALF_OPENstill allow only one trialsrc/lib/circuitBreaker.test.tscontains the regressionSecurity and Failure-Mode Handling
HALF_OPENsingle-trial admission check is unchanged; only the release path is corrected. Callers that do not acquire the slot still fail fast.finallyon the acquiring invocation guarantees the slot is freed whether the trial succeeds (→CLOSED), fails (→OPEN), or throws unexpectedly. This prevents a transient upstream outage from becoming permanent.circuitBreaker.ts; no API, config, or dependency changes.Compatibility
No public API or behavioral contract changes beyond the bug fix. Existing callers see identical semantics for the single-trial guard; the only observable difference is that breakers can now recover across multiple cycles instead of latching after the first.
Closes #1270