Skip to content

Commit 71c04d7

Browse files
fix(vscode-lm): cancel in-flight completions on handler disposal
Review follow-ups: completePrompt token sources are tracked on the handler and cancelled in dispose() (removed from the set in finally), so tearing the handler down stops completion requests as well as streaming ones. completePrompt also adopts the shared hasUsableTimeout/toSdkTimeoutMs helpers instead of inlining ceil/cap. Adds two focused tests: an external abort landing while getClient() is pending rejects with the contract AbortError without a host request, and disposal during client initialization cancels the active completion token. vscode-lm.spec.ts 382 passed; tsc clean for the file.
1 parent 394696a commit 71c04d7

2 files changed

Lines changed: 90 additions & 8 deletions

File tree

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

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4423,6 +4423,77 @@ describe("VsCodeLmHandler", () => {
44234423
}
44244424
})
44254425

4426+
it("should reject with an AbortError when the external signal aborts during client initialization", async () => {
4427+
// Gate client initialization so the abort lands while getClient() is pending.
4428+
let releaseClient: () => void = () => {}
4429+
const clientGate = new Promise<void>((resolve) => {
4430+
releaseClient = resolve
4431+
})
4432+
let notifySelectCalled: () => void = () => {}
4433+
const selectCalled = new Promise<void>((resolve) => {
4434+
notifySelectCalled = resolve
4435+
})
4436+
;(vscode.lm.selectChatModels as Mock).mockImplementation(() => {
4437+
notifySelectCalled()
4438+
return clientGate.then(() => [{ ...mockLanguageModelChat }])
4439+
})
4440+
handler["client"] = null
4441+
4442+
const controller = new AbortController()
4443+
const promise = handler.completePrompt("Test prompt", { abortSignal: controller.signal })
4444+
// Defensive: the abort may reject before the client gate settles.
4445+
void promise.catch(() => {})
4446+
await selectCalled
4447+
controller.abort()
4448+
4449+
const tokenSource = tokenSourceInstance()
4450+
expect(tokenSource.cancel).toHaveBeenCalled()
4451+
releaseClient()
4452+
4453+
await expect(promise).rejects.toSatisfy(
4454+
(error) =>
4455+
error instanceof Error &&
4456+
error.name === "AbortError" &&
4457+
error.message === "The VS Code LM request was aborted",
4458+
)
4459+
expect(mockLanguageModelChat.sendRequest).toHaveBeenCalledTimes(0)
4460+
})
4461+
4462+
it("cancels an active completion request when the handler is disposed during client initialization", async () => {
4463+
// A completion still waiting on client initialization must be cancelled by
4464+
// handler disposal, not left running with no owner.
4465+
let releaseClient: () => void = () => {}
4466+
const clientGate = new Promise<void>((resolve) => {
4467+
releaseClient = resolve
4468+
})
4469+
let notifySelectCalled: () => void = () => {}
4470+
const selectCalled = new Promise<void>((resolve) => {
4471+
notifySelectCalled = resolve
4472+
})
4473+
;(vscode.lm.selectChatModels as Mock).mockImplementation(() => {
4474+
notifySelectCalled()
4475+
return clientGate.then(() => [{ ...mockLanguageModelChat }])
4476+
})
4477+
handler["client"] = null
4478+
4479+
const promise = handler.completePrompt("Test prompt")
4480+
void promise.catch(() => {})
4481+
await selectCalled
4482+
const tokenSource = tokenSourceInstance()
4483+
4484+
handler.dispose()
4485+
4486+
expect(tokenSource.cancel).toHaveBeenCalled()
4487+
releaseClient()
4488+
await expect(promise).rejects.toSatisfy(
4489+
(error) =>
4490+
error instanceof Error &&
4491+
error.name === "AbortError" &&
4492+
error.message === "The VS Code LM request was aborted",
4493+
)
4494+
expect(mockLanguageModelChat.sendRequest).toHaveBeenCalledTimes(0)
4495+
})
4496+
44264497
it("should apply both the abort signal and timeoutMs", async () => {
44274498
const mockModel = { ...mockLanguageModelChat }
44284499
;(vscode.lm.selectChatModels as Mock).mockResolvedValueOnce([mockModel])

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

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import { CONTEXT_WINDOW_EXCEEDED_STATUS } from "../../core/context/context-manag
2828

2929
import { BaseProvider } from "./base-provider"
3030
import { getVsCodeLmModelInfo } from "./vscode-lm-capabilities"
31-
import { createAbortError } from "./utils/abort-signal"
31+
import { createAbortError, hasUsableTimeout, toSdkTimeoutMs } from "./utils/abort-signal"
3232
import type { SingleCompletionHandler, ApiHandlerCreateMessageMetadata, CompletePromptOptions } from "../index"
3333

3434
/**
@@ -770,6 +770,9 @@ export class VsCodeLmHandler extends BaseProvider implements SingleCompletionHan
770770
// Provider label for the shared abort contract (createAbortError).
771771
private readonly providerName = "VS Code LM"
772772
private currentRequestCancellation: vscode.CancellationTokenSource | null
773+
// completePrompt requests do not use currentRequestCancellation (which belongs to
774+
// createMessage); they are tracked here so handler disposal cancels them too.
775+
private activeCompletionTokens = new Set<vscode.CancellationTokenSource>()
773776

774777
constructor(options: ApiHandlerOptions) {
775778
super()
@@ -960,6 +963,14 @@ export class VsCodeLmHandler extends BaseProvider implements SingleCompletionHan
960963
this.currentRequestCancellation.cancel()
961964
this.currentRequestCancellation.dispose()
962965
}
966+
967+
// Cancel every in-flight completion request: a host request still running after the
968+
// handler is disposed would otherwise keep streaming with no owner to stop it.
969+
for (const tokenSource of this.activeCompletionTokens) {
970+
tokenSource.cancel()
971+
tokenSource.dispose()
972+
}
973+
this.activeCompletionTokens.clear()
963974
}
964975

965976
/**
@@ -1638,6 +1649,7 @@ export class VsCodeLmHandler extends BaseProvider implements SingleCompletionHan
16381649
// established before client initialization so the getClient() await is covered
16391650
// by the timeout, the abort bridge, and the finally cleanup below.
16401651
const tokenSource = new vscode.CancellationTokenSource()
1652+
this.activeCompletionTokens.add(tokenSource)
16411653
const externalAbortSignal = options?.abortSignal
16421654

16431655
// Apply the timeout only when the effective value (option, else the provider-wide
@@ -1647,13 +1659,11 @@ export class VsCodeLmHandler extends BaseProvider implements SingleCompletionHan
16471659
// lookup still cancels the request before any sendRequest call.
16481660
let timeoutId: ReturnType<typeof setTimeout> | undefined
16491661
const timeoutMs = options?.timeoutMs ?? this.timeoutMs
1650-
if (Number.isFinite(timeoutMs) && timeoutMs > 0) {
1651-
// Node clamps delays above 2^31-1 ms to 1 ms (and Infinity coerces the
1652-
// same way), which would cancel almost immediately, and truncates
1653-
// fractional delays; round up and cap so the semantics match
1654-
// mergeAbortSignalAndTimeout, and treat non-finite values as
1655-
// "no timeout".
1656-
timeoutId = setTimeout(() => tokenSource.cancel(), Math.min(Math.ceil(timeoutMs), 2_147_483_647))
1662+
if (hasUsableTimeout(timeoutMs)) {
1663+
// hasUsableTimeout/toSdkTimeoutMs own the "disabled" and non-finite semantics:
1664+
// Node clamps large delays to 1 ms and rejects fractional ones, so only a
1665+
// rounded, capped budget reaches setTimeout.
1666+
timeoutId = setTimeout(() => tokenSource.cancel(), toSdkTimeoutMs(timeoutMs))
16571667
}
16581668

16591669
// Bridge the external abort signal: a pre-aborted signal cancels the token
@@ -1729,6 +1739,7 @@ export class VsCodeLmHandler extends BaseProvider implements SingleCompletionHan
17291739
if (externalAbortSignal && onAbort) {
17301740
externalAbortSignal.removeEventListener("abort", onAbort)
17311741
}
1742+
this.activeCompletionTokens.delete(tokenSource)
17321743
tokenSource.dispose()
17331744
}
17341745
}

0 commit comments

Comments
 (0)