Skip to content

Commit 2c34c18

Browse files
committed
fix(api): reject non-finite and cap overflowing timeoutMs delays
Port of the #1300 timeout guard finding (CodeRabbit): Node coerces setTimeout delays above 2^31-1 ms (and Infinity) to 1 ms, so completePrompt's timeout could abort almost immediately. The provider guard now requires Number.isFinite and caps the delay at 2^31-1 ms; the shared mergeAbortSignalAndTimeout helper gets the same treatment (AbortSignal.timeout throws ERR_OUT_OF_RANGE on Infinity and clamps overflowing delays to 1 ms, breaking discovery and merged-signal paths). Regression tests: Infinity schedules no provider timer and means "no timeout" in the helper; overflowing values schedule capped timers. Negative controls fail exactly the new tests per revert.
1 parent e7ca0b0 commit 2c34c18

4 files changed

Lines changed: 70 additions & 8 deletions

File tree

‎src/api/providers/__tests__/native-ollama.spec.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,6 +1035,33 @@ describe("NativeOllamaHandler", () => {
10351035
expect(OllamaMock).toHaveBeenCalledWith(expect.objectContaining({ host: "http://localhost:11434" }))
10361036
})
10371037

1038+
it("should not start a timeout timer for a non-finite timeoutMs", async () => {
1039+
const setTimeoutSpy = vitest.spyOn(global, "setTimeout")
1040+
mockChat.mockResolvedValue({
1041+
message: { content: "Response" },
1042+
})
1043+
1044+
await handler.completePrompt("Test prompt", { timeoutMs: Infinity })
1045+
1046+
// Infinity must mean "no timeout": Node would coerce a non-finite
1047+
// delay to 1ms and abort the request almost immediately.
1048+
expect(setTimeoutSpy).not.toHaveBeenCalled()
1049+
expect(OllamaMock).toHaveBeenCalledTimes(1)
1050+
})
1051+
1052+
it("should cap an overflowing timeoutMs instead of scheduling a 1ms timer", async () => {
1053+
const setTimeoutSpy = vitest.spyOn(global, "setTimeout")
1054+
mockChat.mockResolvedValue({
1055+
message: { content: "Response" },
1056+
})
1057+
1058+
await handler.completePrompt("Test prompt", { timeoutMs: 2_147_483_648 })
1059+
1060+
// 2^31 ms overflows Node's timer clamp to 1ms; the guard caps the
1061+
// delay at 2^31-1 so the request is not aborted within a millisecond.
1062+
expect(setTimeoutSpy).toHaveBeenCalledWith(expect.any(Function), 2_147_483_647)
1063+
})
1064+
10381065
it("should reject with AbortError and remove the abort listener when abortSignal fires", async () => {
10391066
const controller = new AbortController()
10401067
// The timer id is only compared by identity here (clearTimeout is spied),

‎src/api/providers/native-ollama.ts‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -679,11 +679,18 @@ export class NativeOllamaHandler extends BaseProvider implements SingleCompletio
679679
try {
680680
// Handle timeoutMs if provided (client already exists above, so the
681681
// timer can abort both the per-request transport and the client)
682-
if (options?.timeoutMs !== undefined && options.timeoutMs > 0) {
683-
timeoutId = setTimeout(() => {
684-
requestController.abort()
685-
client.abort()
686-
}, options.timeoutMs)
682+
const timeoutMs = options?.timeoutMs
683+
if (timeoutMs !== undefined && Number.isFinite(timeoutMs) && timeoutMs > 0) {
684+
// Node clamps delays above 2^31-1 ms to 1 ms (and coerces
685+
// Infinity the same way), which would abort almost immediately;
686+
// cap the delay and treat non-finite values as "no timeout".
687+
timeoutId = setTimeout(
688+
() => {
689+
requestController.abort()
690+
client.abort()
691+
},
692+
Math.min(timeoutMs, 2_147_483_647),
693+
)
687694
}
688695

689696
// Propagate abortSignal into the per-request client via client.abort()

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

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

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

280303
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)