@@ -87,7 +87,6 @@ import {
8787} from "../../codex/routing" ;
8888import {
8989 applyUpstreamRecoveryInit ,
90- cancelResponseBodyBestEffort ,
9190 fetchWithResetRetry ,
9291 fetchWithTransientRetry ,
9392 sleepWithAbort ,
@@ -1656,7 +1655,8 @@ async function handleResponsesInner(
16561655 rateLimitRetries += 1 ;
16571656 // Release the unread 429 body before the backoff (only the header is needed for the wait).
16581657 const retryAfterHeader = upstreamResponse . headers . get ( "retry-after" ) ;
1659- cancelResponseBodyBestEffort ( upstreamResponse ) ;
1658+ // AWAIT the cancellation so the resource-release guarantee is real, not best-effort.
1659+ try { await upstreamResponse . body ?. cancel ( ) . catch ( ( ) => { } ) ; } catch { /* already closed */ }
16601660 try {
16611661 await sleepWithAbort (
16621662 rateLimitRetryDelayMs ( rateLimitPolicy , retryAfterHeader , Date . now ( ) ) ,
@@ -1666,6 +1666,12 @@ async function handleResponsesInner(
16661666 upstream . abort ( ) ;
16671667 return clientCancelledResponse ( ) ;
16681668 }
1669+ // Client cancellation wins over any stale timer edge: re-check before dispatching the
1670+ // replay so the wire never starts work for a request the client already abandoned.
1671+ if ( options . abortSignal ?. aborted || upstream . signal . aborted ) {
1672+ upstream . abort ( ) ;
1673+ return clientCancelledResponse ( ) ;
1674+ }
16691675 try {
16701676 upstreamResponse = await fetchWithTransientRetry (
16711677 recovery => {
@@ -2355,6 +2361,12 @@ async function handleResponsesInner(
23552361 request . releaseBodyObservation ?.( ) ;
23562362 }
23572363
2364+ // Same-target 429 retry budget is per REQUEST: it lives OUTSIDE the recovery loop (so a 413/401
2365+ // replay that comes back 429 cannot silently re-arm a fresh budget) and is SHARED with the
2366+ // terminal-guard continuation below, so the main loop + one continuation can never exceed
2367+ // `attempts` same-key replays in total (bounded per request).
2368+ const rateLimitPolicy = rateLimitRetryPolicyFor ( route . provider ) ;
2369+ let rateLimitRetries = 0 ;
23582370 if ( ! upstreamResponse . ok ) {
23592371 // Recovery loop: multi-key 429 failover + at most ONE anthropic 413 tightened retry
23602372 // (devlog/260714_image_normalization_pipeline/030). One mutable activeAdapter serves
@@ -2364,10 +2376,6 @@ async function handleResponsesInner(
23642376 let imageTierBias = 0 ;
23652377 let imageRetryAttempted = false ;
23662378 let oauth401ReplayAttempted = false ;
2367- // Same-target 429 retry budget lives OUTSIDE the recovery loop so a 413/401 replay that
2368- // comes back 429 cannot silently re-arm a fresh budget (bounded to `attempts` per request).
2369- const rateLimitPolicy = rateLimitRetryPolicyFor ( route . provider ) ;
2370- let rateLimitRetries = 0 ;
23712379 /**
23722380 * Rebuild the request from the current parsed input (and any image-tier bias) and refetch
23732381 * it once, tagging the attempt with the given recovery kind. Rebuilds are deterministic
@@ -2462,7 +2470,8 @@ async function handleResponsesInner(
24622470 // wait, and under a 429 storm the sockets would otherwise accumulate for the whole
24632471 // configured interval (same pattern as the key-failover branch below).
24642472 const retryAfterHeader = upstreamResponse . headers . get ( "retry-after" ) ;
2465- cancelResponseBodyBestEffort ( upstreamResponse ) ;
2473+ // AWAIT the cancellation so the resource-release guarantee is real, not best-effort.
2474+ try { await upstreamResponse . body ?. cancel ( ) . catch ( ( ) => { } ) ; } catch { /* already closed */ }
24662475 try {
24672476 await sleepWithAbort (
24682477 rateLimitRetryDelayMs ( rateLimitPolicy , retryAfterHeader , Date . now ( ) ) ,
@@ -2473,6 +2482,13 @@ async function handleResponsesInner(
24732482 upstream . abort ( ) ;
24742483 return clientCancelledResponse ( ) ;
24752484 }
2485+ // Client cancellation wins over any stale timer edge: re-check before dispatching the
2486+ // replay so an adapter never starts work for a request the client already abandoned.
2487+ if ( options . abortSignal ?. aborted || upstream . signal . aborted ) {
2488+ cleanupUpstreamAbort ( ) ;
2489+ upstream . abort ( ) ;
2490+ return clientCancelledResponse ( ) ;
2491+ }
24762492 const result = await rebuildAndRefetch ( "rate-limit-429" ) ;
24772493 if ( "failed" in result ) return result . failed ;
24782494 upstreamResponse = result ;
@@ -2601,11 +2617,6 @@ async function handleResponsesInner(
26012617 const fetchTerminalGuardContinuation = async function * ( nextParsed : OcxParsedRequest ) : AsyncGenerator < AdapterEvent > {
26022618 let imageTierBias = 0 ;
26032619 let response : Response | undefined ;
2604- // Same-target 429 budget is per REQUEST, not per failover iteration: a key/account rotation
2605- // or a 413 tier retry that comes back 429 must not re-arm a fresh budget (parity with the
2606- // main recovery loop above).
2607- const rateLimitPolicy = rateLimitRetryPolicyFor ( route . provider ) ;
2608- let rateLimitRetries = 0 ;
26092620 /**
26102621 * Build and fetch one terminal-guard continuation. `replay` marks a same-target 429
26112622 * replay so its send records the `rate-limit-429` recovery kind; the adapter rebuild is
@@ -2676,7 +2687,8 @@ async function handleResponsesInner(
26762687 rateLimitRetries += 1 ;
26772688 // Release the unread 429 body before the backoff (only the header is needed for the wait).
26782689 const retryAfterHeader = response . headers . get ( "retry-after" ) ;
2679- try { void response . body ?. cancel ( ) . catch ( ( ) => { } ) ; } catch { /* already closed */ }
2690+ // AWAIT the cancellation so the resource-release guarantee is real, not best-effort.
2691+ try { await response . body ?. cancel ( ) . catch ( ( ) => { } ) ; } catch { /* already closed */ }
26802692 try {
26812693 await sleepWithAbort (
26822694 rateLimitRetryDelayMs ( rateLimitPolicy , retryAfterHeader , Date . now ( ) ) ,
@@ -2693,6 +2705,12 @@ async function handleResponsesInner(
26932705 }
26942706 return ;
26952707 }
2708+ // Client cancellation wins over any stale timer edge: re-check before dispatching the
2709+ // replay so the continuation never starts work for a request the client abandoned.
2710+ if ( options . abortSignal ?. aborted || upstream . signal . aborted ) {
2711+ yield { type : "error" , message : "client closed request during terminal continuation" , status : 499 } ;
2712+ return ;
2713+ }
26962714 try {
26972715 response = await fetchContinuation ( true ) ;
26982716 } catch ( error ) {
0 commit comments