Skip to content

Commit a689e5a

Browse files
anandgupta42claude
andcommitted
fix(provider): settle a stuck custom fetch on caller cancel too
Race the custom fetch against the combined request signal (first-byte deadline, caller cancel, timeouts), not only the deadline, so a Stop is not held up by pre-request work that ignores the signal. The tool-race test now waits for the pending tool part before reporting the execution started. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent b852ba4 commit a689e5a

3 files changed

Lines changed: 39 additions & 9 deletions

File tree

‎packages/opencode/src/provider/provider.ts‎

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2037,22 +2037,27 @@ export namespace Provider {
20372037
timeout: false,
20382038
}))()
20392039
let res: Response
2040-
if (headerTimeoutCtl) {
2041-
const signal = headerTimeoutCtl.signal
2042-
let onAbort = () => {}
2040+
if (opts.signal) {
2041+
// settle on the combined abort (first-byte deadline, caller cancel, timeouts), whichever comes first
2042+
const guards = [opts.signal]
2043+
const cleanups: Array<() => void> = []
20432044
const deadline = new Promise<never>((_, reject) => {
2044-
onAbort = () => reject(signal.reason)
2045-
signal.addEventListener("abort", onAbort, { once: true })
2045+
for (const guard of guards) {
2046+
const onAbort = () => reject(guard.reason)
2047+
if (guard.aborted) return onAbort()
2048+
guard.addEventListener("abort", onAbort, { once: true })
2049+
cleanups.push(() => guard.removeEventListener("abort", onAbort))
2050+
}
20462051
})
20472052
pending.catch(() => {}) // the loser of the race must not surface as an unhandled rejection
20482053
try {
20492054
res = await Promise.race([pending, deadline])
20502055
} finally {
2051-
signal.removeEventListener("abort", onAbort)
2052-
headerTimeoutCtl.clear()
2056+
for (const cleanup of cleanups) cleanup()
2057+
headerTimeoutCtl?.clear()
20532058
}
20542059
} else {
2055-
res = await pending
2060+
res = await pending.finally(() => headerTimeoutCtl?.clear())
20562061
}
20572062
// altimate_change end
20582063

‎packages/opencode/test/release-validation/chunk-timeout-844.test.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,4 +295,27 @@ describe("first-byte timeout bounds fetch work that ignores the abort signal", (
295295
},
296296
})
297297
})
298+
test("a caller cancel also settles a fetch that ignores the signal", async () => {
299+
await using tmp = await tmpdir({
300+
init: (dir) => writeCapturingProvider(dir, { apiKey: "x", headerTimeout: 60_000 }),
301+
})
302+
await Instance.provide({
303+
directory: tmp.path,
304+
fn: async () => {
305+
const wrapper = await captureWrapper()
306+
const origFetch = globalThis.fetch
307+
globalThis.fetch = (() => new Promise(() => {})) as any
308+
try {
309+
const ctl = new AbortController()
310+
const started = Date.now()
311+
const call = wrapper("http://x/v1/chat", { method: "POST", signal: ctl.signal })
312+
setTimeout(() => ctl.abort(new DOMException("Aborted", "AbortError")), 100)
313+
await expect(call).rejects.toThrow("Aborted")
314+
expect(Date.now() - started).toBeLessThan(3000) // far below the 60s deadline
315+
} finally {
316+
globalThis.fetch = origFetch
317+
}
318+
},
319+
})
320+
})
298321
})

‎packages/opencode/test/session/stall-recovery.test.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,9 @@ function runSession(opts: RunOptions) {
299299
}
300300
if (opts.onFirstRequest) {
301301
const poll = setInterval(() => {
302-
if (server.requests === 0) return
302+
// only once the stalled attempt has persisted its (still pending) tool part
303+
const pending = MessageV2.parts(msg.id).some((p) => p.type === "tool" && p.state.status === "pending")
304+
if (server.requests === 0 || !pending) return
303305
clearInterval(poll)
304306
opts.onFirstRequest!(handle)
305307
}, 5)

0 commit comments

Comments
 (0)