Skip to content

Commit 0f23d98

Browse files
committed
fix(api): reject non-finite and cap overflowing mergeAbortSignalAndTimeout delays
Port of the #1300 timeout guard finding (CodeRabbit): Node's AbortSignal.timeout throws ERR_OUT_OF_RANGE for non-integer delays (Infinity) and clamps delays above 2^31-1 ms to 1 ms, so a non-finite timeoutMs threw a RangeError instead of running the request, and an overflowing one aborted almost immediately. The helper now treats non-finite values as "no timeout" and caps finite delays at 2^31-1 ms. Regression tests added; negative controls (reverting the guard) fail exactly the two new tests.
1 parent 888cefe commit 0f23d98

2 files changed

Lines changed: 31 additions & 3 deletions

File tree

‎src/api/providers/utils/__tests__/abort-signal.spec.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,29 @@ describe("abort-signal utilities", () => {
276276

277277
await vi.waitFor(() => expect(result?.aborted).toBe(true))
278278
})
279+
280+
it("treats a non-finite timeoutMs as no timeout", () => {
281+
const controller = new AbortController()
282+
283+
// Infinity must not reach AbortSignal.timeout(), which throws
284+
// ERR_OUT_OF_RANGE for non-integer delays.
285+
expect(() => mergeAbortSignalAndTimeout(undefined, Infinity)).not.toThrow()
286+
expect(mergeAbortSignalAndTimeout(undefined, Infinity)).toBeUndefined()
287+
expect(mergeAbortSignalAndTimeout(controller.signal, Infinity)).toBe(controller.signal)
288+
})
289+
290+
it("caps an overflowing timeoutMs instead of aborting immediately", async () => {
291+
// Node clamps AbortSignal.timeout delays above 2^31-1 ms to 1 ms;
292+
// the helper caps the delay so an overflowing timeout does not abort
293+
// the request within the first millisecond.
294+
const result = mergeAbortSignalAndTimeout(undefined, 2_147_483_648)
295+
296+
expect(result).toBeInstanceOf(AbortSignal)
297+
expect(result?.aborted).toBe(false)
298+
299+
await new Promise((resolve) => setTimeout(resolve, 20))
300+
expect(result?.aborted).toBe(false)
301+
})
279302
})
280303

281304
describe("mergeAbortSignals", () => {

‎src/api/providers/utils/abort-signal.ts‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
/**
22
* Merge an optional external abort signal with an optional timeout.
33
*
4-
* Timeout values <= 0 are treated as disabled. The timeout is created via the
4+
* Timeout values <= 0 and non-finite values are treated as disabled; finite
5+
* delays above 2^31-1 ms are capped (Node clamps them to 1 ms otherwise). The timeout is created via the
56
* native AbortSignal.timeout() API, which self-manages its timer — callers do
67
* not need to (and cannot) clear it manually.
78
*/
89
export function mergeAbortSignalAndTimeout(externalSignal?: AbortSignal, timeoutMs?: number): AbortSignal | undefined {
9-
const hasTimeout = typeof timeoutMs === "number" && timeoutMs > 0
10+
// Node's AbortSignal.timeout() throws ERR_OUT_OF_RANGE for non-integer
11+
// delays (Infinity) and clamps delays above 2^31-1 ms to 1 ms, which would
12+
// abort almost immediately; treat non-finite values as "no timeout" and
13+
// cap the rest.
14+
const hasTimeout = typeof timeoutMs === "number" && Number.isFinite(timeoutMs) && timeoutMs > 0
1015

1116
if (!hasTimeout) {
1217
return externalSignal
1318
}
1419

15-
const timeoutSignal = AbortSignal.timeout(timeoutMs)
20+
const timeoutSignal = AbortSignal.timeout(Math.min(timeoutMs, 2_147_483_647))
1621

1722
if (!externalSignal) {
1823
return timeoutSignal

0 commit comments

Comments
 (0)