Skip to content

Commit 8741154

Browse files
committed
fix(api): cancel vscode-lm request on premature generator closure + fix pre-abort test comment
1 parent 4e72095 commit 8741154

2 files changed

Lines changed: 43 additions & 2 deletions

File tree

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

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,40 @@ describe("VsCodeLmHandler", () => {
624624
expect(removeEventListenerSpy).toHaveBeenCalledWith("abort", expect.any(Function))
625625
})
626626

627+
it("should cancel the request token when the consumer stops consuming early", async () => {
628+
const systemPrompt = "You are a helpful assistant"
629+
const messages: Anthropic.Messages.MessageParam[] = [{ role: "user" as const, content: "Hello" }]
630+
631+
// The mock stream yields one chunk and then stays pending, so the host
632+
// request is still in flight when the consumer gives up.
633+
let releaseStream: () => void = () => {}
634+
const streamGate = new Promise<void>((resolve) => {
635+
releaseStream = resolve
636+
})
637+
mockLanguageModelChat.sendRequest.mockResolvedValueOnce({
638+
stream: (async function* () {
639+
yield new vscode.LanguageModelTextPart("Hello")
640+
await streamGate
641+
})(),
642+
text: (async function* () {
643+
yield "Hello"
644+
await streamGate
645+
})(),
646+
})
647+
648+
const stream = handler.createMessage(systemPrompt, messages)
649+
await stream.next()
650+
// Stop consuming before the stream finishes: the premature closure must
651+
// cancel the request token so the host stops the request (dispose alone
652+
// frees resources without cancelling).
653+
// (the AsyncGenerator type requires the return value argument)
654+
await stream.return(undefined)
655+
656+
const tokenSource = tokenSourceInstance()
657+
expect(tokenSource.cancel).toHaveBeenCalled()
658+
expect(tokenSource.dispose).toHaveBeenCalled()
659+
})
660+
627661
it("should throw a Zoo Code branded error on stream error with error-like object", async () => {
628662
const systemPrompt = "You are a helpful assistant"
629663
const messages: Anthropic.Messages.MessageParam[] = [
@@ -1279,9 +1313,10 @@ describe("VsCodeLmHandler", () => {
12791313
const promise = handler.completePrompt("Test prompt", { abortSignal: controller.signal })
12801314
await expect(promise).rejects.toSatisfy((error) => error instanceof Error && error.name === "AbortError")
12811315

1282-
// Fails fast before invoking the host: no cancellation token source is
1283-
// created and the sendRequest is never called.
1316+
// Fails fast before invoking the host: the pre-aborted signal cancels the
1317+
// request token and sendRequest is never called.
12841318
expect(mockLanguageModelChat.sendRequest).toHaveBeenCalledTimes(0)
1319+
expect(tokenSourceInstance().cancel).toHaveBeenCalled()
12851320
})
12861321

12871322
it("should reject with an AbortError when the signal aborts mid-flight", async () => {

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,12 @@ export class VsCodeLmHandler extends BaseProvider implements SingleCompletionHan
575575
if (onExternalAbort !== undefined) {
576576
externalAbortSignal?.removeEventListener("abort", onExternalAbort)
577577
}
578+
// Cancel before disposing: VS Code's CancellationTokenSource.dispose()
579+
// frees resources without cancelling the token, so a premature consumer
580+
// closure (break/return) would otherwise leave the host request running and
581+
// consuming model quota. Cancel is idempotent, so this is a no-op on paths
582+
// where the token was already cancelled (aborted or timed-out request).
583+
cancellationTokenSource.cancel()
578584
// Dispose the request-local source. Clear the shared field only if it still
579585
// points at this request's source: a newer request may have replaced it, and
580586
// disposing the shared field here would cancel and dispose the newer request's

0 commit comments

Comments
 (0)