Skip to content

Commit 7765274

Browse files
committed
fix(api): reject non-finite and cap overflowing completePrompt timeouts
CodeRabbit: the timeoutMs guard let Infinity and values above 2^31-1 through to setTimeout, which Node coerces to a 1ms delay - completePrompt could then cancel almost immediately with an AbortError. Non-finite values now mean "no timeout" and finite delays are capped at 2^31-1 ms. Regression tests: Infinity schedules no timer and completes; 2^31 ms schedules a capped timer that does not fire within the first second. Negative controls: reverting the guard fails each test in isolation (1 red per control).
1 parent 589b64b commit 7765274

2 files changed

Lines changed: 86 additions & 2 deletions

File tree

‎src/api/providers/__tests__/vscode-lm.spec.ts‎

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4253,6 +4253,86 @@ describe("VsCodeLmHandler", () => {
42534253
}
42544254
})
42554255

4256+
it("should not treat an Infinity timeoutMs as an immediate timeout", async () => {
4257+
let releaseStream: () => void = () => {}
4258+
const streamGate = new Promise<void>((resolve) => {
4259+
releaseStream = resolve
4260+
})
4261+
mockLanguageModelChat.sendRequest.mockResolvedValueOnce({
4262+
stream: (async function* () {
4263+
await streamGate
4264+
yield new vscode.LanguageModelTextPart("Completed text")
4265+
})(),
4266+
text: (async function* () {
4267+
await streamGate
4268+
yield "Completed text"
4269+
})(),
4270+
})
4271+
4272+
handler["client"] = mockLanguageModelChat
4273+
4274+
vi.useFakeTimers()
4275+
try {
4276+
const promise = handler.completePrompt("Test prompt", { timeoutMs: Infinity })
4277+
for (let i = 0; i < 20; i++) {
4278+
await Promise.resolve()
4279+
}
4280+
expect(mockLanguageModelChat.sendRequest).toHaveBeenCalledTimes(1)
4281+
4282+
// Infinity must mean "no timeout": Node would coerce a non-finite delay to 1ms, so the guard must reject non-finite values before setTimeout.
4283+
expect(vi.getTimerCount()).toBe(0)
4284+
await vi.advanceTimersByTimeAsync(1000)
4285+
expect(tokenSourceInstance().token.isCancellationRequested).toBe(false)
4286+
4287+
releaseStream()
4288+
const result = await promise
4289+
expect(result).toBe("Completed text")
4290+
expect(tokenSourceInstance().token.isCancellationRequested).toBe(false)
4291+
} finally {
4292+
vi.useRealTimers()
4293+
}
4294+
})
4295+
4296+
it("should cap an overflowing timeoutMs instead of firing immediately", async () => {
4297+
let releaseStream: () => void = () => {}
4298+
const streamGate = new Promise<void>((resolve) => {
4299+
releaseStream = resolve
4300+
})
4301+
mockLanguageModelChat.sendRequest.mockResolvedValueOnce({
4302+
stream: (async function* () {
4303+
await streamGate
4304+
yield new vscode.LanguageModelTextPart("Completed text")
4305+
})(),
4306+
text: (async function* () {
4307+
await streamGate
4308+
yield "Completed text"
4309+
})(),
4310+
})
4311+
4312+
handler["client"] = mockLanguageModelChat
4313+
4314+
vi.useFakeTimers()
4315+
try {
4316+
const promise = handler.completePrompt("Test prompt", { timeoutMs: 2_147_483_648 })
4317+
for (let i = 0; i < 20; i++) {
4318+
await Promise.resolve()
4319+
}
4320+
expect(mockLanguageModelChat.sendRequest).toHaveBeenCalledTimes(1)
4321+
4322+
// 2^31 ms overflows Node's timer clamp to 1ms; the guard caps the delay at 2^31-1 so the request is not cancelled within the first second.
4323+
expect(vi.getTimerCount()).toBe(1)
4324+
await vi.advanceTimersByTimeAsync(1000)
4325+
expect(tokenSourceInstance().token.isCancellationRequested).toBe(false)
4326+
4327+
releaseStream()
4328+
const result = await promise
4329+
expect(result).toBe("Completed text")
4330+
expect(tokenSourceInstance().token.isCancellationRequested).toBe(false)
4331+
} finally {
4332+
vi.useRealTimers()
4333+
}
4334+
})
4335+
42564336
it("should clear the timeout timer when the completion finishes before it elapses", async () => {
42574337
mockLanguageModelChat.sendRequest.mockResolvedValueOnce({
42584338
stream: (async function* () {

‎src/api/providers/vscode-lm.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1562,8 +1562,12 @@ export class VsCodeLmHandler extends BaseProvider implements SingleCompletionHan
15621562
// the timer before getClient() means a timeout that fires during a slow client
15631563
// lookup still cancels the request before any sendRequest call.
15641564
let timeoutId: ReturnType<typeof setTimeout> | undefined
1565-
if (options?.timeoutMs !== undefined && options.timeoutMs > 0) {
1566-
timeoutId = setTimeout(() => tokenSource.cancel(), options.timeoutMs)
1565+
const timeoutMs = options?.timeoutMs
1566+
if (timeoutMs !== undefined && Number.isFinite(timeoutMs) && timeoutMs > 0) {
1567+
// Node clamps delays above 2^31-1 ms to 1 ms (and Infinity coerces the
1568+
// same way), which would cancel almost immediately; cap the delay and
1569+
// treat non-finite values as "no timeout".
1570+
timeoutId = setTimeout(() => tokenSource.cancel(), Math.min(timeoutMs, 2_147_483_647))
15671571
}
15681572

15691573
// Bridge the external abort signal: a pre-aborted signal cancels the token

0 commit comments

Comments
 (0)