Skip to content

Commit f160a99

Browse files
committed
fix(api): round fractional timeouts before AbortSignal.timeout
CodeRabbit follow-up on the timeout guard: Node validates AbortSignal.timeout delays as uint32, so finite fractions (1500.5, 0.5) threw ERR_OUT_OF_RANGE just like Infinity did. The helper now rounds delays up (a positive fraction never becomes 0 ms) before capping at 2^31-1 ms. Regression test mergeAbortSignalAndTimeout (undefined, 1.5); negative control (reverting the rounding) fails exactly that test.
1 parent 0f23d98 commit f160a99

2 files changed

Lines changed: 18 additions & 3 deletions

File tree

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,20 @@ describe("abort-signal utilities", () => {
287287
expect(mergeAbortSignalAndTimeout(controller.signal, Infinity)).toBe(controller.signal)
288288
})
289289

290+
it("rounds a fractional timeoutMs up before scheduling", async () => {
291+
// Node validates AbortSignal.timeout delays as uint32: 1.5 or 0.5
292+
// would throw ERR_OUT_OF_RANGE. The helper rounds up, so a positive
293+
// fraction never becomes 0 ms either.
294+
let result: AbortSignal | undefined
295+
expect(() => {
296+
result = mergeAbortSignalAndTimeout(undefined, 1.5)
297+
}).not.toThrow()
298+
expect(result).toBeInstanceOf(AbortSignal)
299+
expect(result?.aborted).toBe(false)
300+
await vi.waitFor(() => expect(result?.aborted).toBe(true))
301+
expect(() => mergeAbortSignalAndTimeout(undefined, 0.5)).not.toThrow()
302+
})
303+
290304
it("caps an overflowing timeoutMs instead of aborting immediately", async () => {
291305
// Node clamps AbortSignal.timeout delays above 2^31-1 ms to 1 ms;
292306
// the helper caps the delay so an overflowing timeout does not abort

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,16 @@
99
export function mergeAbortSignalAndTimeout(externalSignal?: AbortSignal, timeoutMs?: number): AbortSignal | undefined {
1010
// Node's AbortSignal.timeout() throws ERR_OUT_OF_RANGE for non-integer
1111
// 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.
12+
// abort almost immediately; treat non-finite values as "no timeout",
13+
// round fractional delays up to whole milliseconds (uint32 validation
14+
// throws for 1500.5), and cap the rest.
1415
const hasTimeout = typeof timeoutMs === "number" && Number.isFinite(timeoutMs) && timeoutMs > 0
1516

1617
if (!hasTimeout) {
1718
return externalSignal
1819
}
1920

20-
const timeoutSignal = AbortSignal.timeout(Math.min(timeoutMs, 2_147_483_647))
21+
const timeoutSignal = AbortSignal.timeout(Math.min(Math.max(1, Math.ceil(timeoutMs)), 2_147_483_647))
2122

2223
if (!externalSignal) {
2324
return timeoutSignal

0 commit comments

Comments
 (0)