Skip to content

Commit 8101d01

Browse files
committed
fix(api): round fractional completion timeouts up before scheduling
CodeRabbit: the raw setTimeout path passed 1500.5 through and Node truncates non-integer delays, cancelling at 1500 ms - inconsistent with the PR objective and mergeAbortSignalAndTimeout's 1501. The guard now applies Math.ceil before the cap. New test pins the delay handed to setTimeout (1501 for 1500.5); negative control (reverting the ceil) fails exactly that test.
1 parent 71aa9db commit 8101d01

2 files changed

Lines changed: 31 additions & 3 deletions

File tree

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

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

4256+
it("should schedule a fractional timeoutMs cancellation rounded up", async () => {
4257+
// Node truncates non-integer setTimeout delays (1500.5 fires at
4258+
// 1500 ms); the guard must round up so the delay matches
4259+
// mergeAbortSignalAndTimeout's 1501. Pin the delay handed to
4260+
// setTimeout rather than relying on fake-timer semantics.
4261+
const setTimeoutSpy = vi.spyOn(global, "setTimeout")
4262+
mockLanguageModelChat.sendRequest.mockResolvedValueOnce({
4263+
stream: (async function* () {
4264+
yield new vscode.LanguageModelTextPart("Completed text")
4265+
})(),
4266+
text: (async function* () {
4267+
yield "Completed text"
4268+
})(),
4269+
})
4270+
4271+
handler["client"] = mockLanguageModelChat
4272+
4273+
try {
4274+
const result = await handler.completePrompt("Test prompt", { timeoutMs: 1500.5 })
4275+
expect(result).toBe("Completed text")
4276+
expect(setTimeoutSpy).toHaveBeenCalledWith(expect.any(Function), 1501)
4277+
} finally {
4278+
setTimeoutSpy.mockRestore()
4279+
}
4280+
})
4281+
42564282
it("should not treat an Infinity timeoutMs as an immediate timeout", async () => {
42574283
let releaseStream: () => void = () => {}
42584284
const streamGate = new Promise<void>((resolve) => {

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1565,9 +1565,11 @@ export class VsCodeLmHandler extends BaseProvider implements SingleCompletionHan
15651565
const timeoutMs = options?.timeoutMs
15661566
if (timeoutMs !== undefined && Number.isFinite(timeoutMs) && timeoutMs > 0) {
15671567
// 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))
1568+
// same way), which would cancel almost immediately, and truncates
1569+
// fractional delays; round up and cap so the semantics match
1570+
// mergeAbortSignalAndTimeout, and treat non-finite values as
1571+
// "no timeout".
1572+
timeoutId = setTimeout(() => tokenSource.cancel(), Math.min(Math.ceil(timeoutMs), 2_147_483_647))
15711573
}
15721574

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

0 commit comments

Comments
 (0)