Skip to content

Commit 4e72095

Browse files
committed
fix(api): start vscode-lm request cancellation before client initialization
1 parent aea9464 commit 4e72095

2 files changed

Lines changed: 110 additions & 46 deletions

File tree

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

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,8 @@ describe("VsCodeLmHandler", () => {
513513
const messages: Anthropic.Messages.MessageParam[] = [{ role: "user" as const, content: "Hello" }]
514514

515515
// Gate model selection so the generator waits inside getClient(): the
516-
// pre-abort check and listener attachment happened before this window.
516+
// cancellation token source and the abort-bridge listener are established
517+
// before this window, and the finally cleanup covers the getClient() await.
517518
let releaseClient: () => void = () => {}
518519
const clientGate = new Promise<void>((resolve) => {
519520
releaseClient = resolve
@@ -524,25 +525,32 @@ describe("VsCodeLmHandler", () => {
524525
handler["client"] = null
525526

526527
const controller = new AbortController()
528+
const removeEventListenerSpy = vi.spyOn(controller.signal, "removeEventListener")
527529
const stream = handler.createMessage(
528530
systemPrompt,
529531
messages,
530532
makeCreateMessageMetadata({ abortSignal: controller.signal }),
531533
)
532534
const firstChunk = stream.next()
533535

534-
// Release the client, then abort before the generator can reach sendRequest.
536+
// Release the client, then abort before the next microtask can reach
537+
// countTokens/sendRequest.
535538
releaseClient()
536539
controller.abort()
537540

538541
await expect(firstChunk).rejects.toSatisfy((error) => error instanceof Error && error.name === "AbortError")
539542

540-
// The abort landed after client initialization, so no host request started.
543+
// The abort landed after client initialization, so no host request started
544+
// and no input tokens were counted (the post-init re-check bails first).
541545
expect(handler["client"]).not.toBeNull()
542546
expect(mockLanguageModelChat.sendRequest).toHaveBeenCalledTimes(0)
547+
expect(mockLanguageModelChat.countTokens).toHaveBeenCalledTimes(0)
543548

544-
// The local token source was cancelled for the request that never started.
549+
// The local token source was cancelled for the request that never started,
550+
// and the abort-bridge listener was removed by the finally covering the
551+
// initialization window.
545552
expect(tokenSourceInstance().cancel).toHaveBeenCalled()
553+
expect(removeEventListenerSpy).toHaveBeenCalledTimes(1)
546554
})
547555

548556
it("should bridge a mid-flight external abort to the request cancellation token", async () => {
@@ -1349,6 +1357,39 @@ describe("VsCodeLmHandler", () => {
13491357
}
13501358
})
13511359

1360+
it("should reject with an AbortError when the timeout fires during client initialization", async () => {
1361+
// Gate client initialization so the timeout timer (started before getClient())
1362+
// can fire while getClient() is still pending.
1363+
let releaseClient: () => void = () => {}
1364+
const clientGate = new Promise<void>((resolve) => {
1365+
releaseClient = resolve
1366+
})
1367+
;(vscode.lm.selectChatModels as Mock).mockImplementation(() =>
1368+
clientGate.then(() => [{ ...mockLanguageModelChat }]),
1369+
)
1370+
handler["client"] = null
1371+
1372+
vi.useFakeTimers()
1373+
try {
1374+
const promise = handler.completePrompt("Test prompt", { timeoutMs: 5000 })
1375+
1376+
// Advance past the timeout while getClient() is still pending: the timer
1377+
// fires and cancels the request token.
1378+
await vi.advanceTimersByTimeAsync(5000)
1379+
expect(tokenSourceInstance().cancel).toHaveBeenCalled()
1380+
1381+
// Release the client; the post-init re-check must abort before sendRequest.
1382+
releaseClient()
1383+
1384+
await expect(promise).rejects.toSatisfy(
1385+
(error) => error instanceof Error && error.name === "AbortError",
1386+
)
1387+
expect(mockLanguageModelChat.sendRequest).toHaveBeenCalledTimes(0)
1388+
} finally {
1389+
vi.useRealTimers()
1390+
}
1391+
})
1392+
13521393
it("should apply both the abort signal and timeoutMs", async () => {
13531394
const mockModel = { ...mockLanguageModelChat }
13541395
;(vscode.lm.selectChatModels as Mock).mockResolvedValueOnce([mockModel])

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

Lines changed: 65 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -376,31 +376,15 @@ export class VsCodeLmHandler extends BaseProvider implements SingleCompletionHan
376376
// Ensure clean state before starting a new request
377377
this.ensureCleanState()
378378

379-
// The VS Code LanguageModelChat API cannot carry an AbortSignal, so a
379+
// The VS Code LanguageModelChat API cannot carry an AbortSignal, so the
380+
// request cancellation is established before client initialization and a
380381
// pre-aborted external signal is reported immediately instead of being
381382
// sent to the host.
382383
const externalAbortSignal = metadata?.abortSignal
383-
if (externalAbortSignal?.aborted) {
384-
const abortError = new Error("Zoo Code <Language Model API>: Request aborted")
385-
abortError.name = "AbortError"
386-
throw abortError
387-
}
388-
389-
const client: vscode.LanguageModelChat = await this.getClient()
390-
391-
// Process messages
392-
const cleanedMessages = messages.map((msg) => ({
393-
...msg,
394-
content: this.cleanMessageContent(msg.content),
395-
}))
396-
397-
// Convert Anthropic messages to VS Code LM messages
398-
const vsCodeLmMessages: vscode.LanguageModelChatMessage[] = [
399-
vscode.LanguageModelChatMessage.Assistant(systemPrompt),
400-
...convertToVsCodeLmMessages(cleanedMessages),
401-
]
402384

403-
// Initialize cancellation token for the request
385+
// Initialize cancellation token for the request before getClient() so the
386+
// client-initialization await is covered by the abort bridge and the
387+
// finally cleanup below.
404388
this.currentRequestCancellation = new vscode.CancellationTokenSource()
405389
const cancellationTokenSource = this.currentRequestCancellation
406390

@@ -415,23 +399,46 @@ export class VsCodeLmHandler extends BaseProvider implements SingleCompletionHan
415399
externalAbortSignal.addEventListener("abort", onExternalAbort, { once: true })
416400
}
417401

418-
// Calculate input tokens before starting the stream
419-
const totalInputTokens: number = await this.calculateTotalInputTokens(vsCodeLmMessages)
420-
421402
// Accumulate the text and count at the end of the stream to reduce token counting overhead.
422403
let accumulatedText: string = ""
423404

424405
try {
425-
// Re-check after client initialization: the external signal may have
426-
// aborted while getClient() (or the token calculation) was pending. Bail
427-
// before invoking the host so no request starts for a cancelled request.
406+
// Fail fast if the caller already aborted before we even started: do not
407+
// initialize or invoke the host request for a cancelled request.
408+
if (externalAbortSignal?.aborted) {
409+
cancellationTokenSource.cancel()
410+
const abortError = new Error("Zoo Code <Language Model API>: Request aborted")
411+
abortError.name = "AbortError"
412+
throw abortError
413+
}
414+
415+
const client: vscode.LanguageModelChat = await this.getClient()
416+
417+
// Re-check immediately after client initialization: the signal may have
418+
// aborted while getClient() was pending. Bail before counting input
419+
// tokens or invoking the host so no work happens for a cancelled request.
428420
if (externalAbortSignal?.aborted) {
429421
cancellationTokenSource.cancel()
430422
const abortError = new Error("Zoo Code <Language Model API>: Request aborted")
431423
abortError.name = "AbortError"
432424
throw abortError
433425
}
434426

427+
// Process messages
428+
const cleanedMessages = messages.map((msg) => ({
429+
...msg,
430+
content: this.cleanMessageContent(msg.content),
431+
}))
432+
433+
// Convert Anthropic messages to VS Code LM messages
434+
const vsCodeLmMessages: vscode.LanguageModelChatMessage[] = [
435+
vscode.LanguageModelChatMessage.Assistant(systemPrompt),
436+
...convertToVsCodeLmMessages(cleanedMessages),
437+
]
438+
439+
// Calculate input tokens before starting the stream
440+
const totalInputTokens: number = await this.calculateTotalInputTokens(vsCodeLmMessages)
441+
435442
// Create the response stream with required options
436443
const requestOptions: vscode.LanguageModelChatRequestOptions = {
437444
justification: `Zoo Code would like to use '${client.name}' from '${client.vendor}', Click 'Allow' to proceed.`,
@@ -522,6 +529,16 @@ export class VsCodeLmHandler extends BaseProvider implements SingleCompletionHan
522529
outputTokens: totalOutputTokens,
523530
}
524531
} catch (error: unknown) {
532+
// When the external signal wins during client initialization (the signal
533+
// was already aborted while getClient() was pending or rejected), surface a
534+
// standard abort error instead of wrapping the getClient() failure as a
535+
// generic stream error.
536+
if (externalAbortSignal?.aborted) {
537+
const abortError = new Error("Zoo Code <Language Model API>: Request aborted")
538+
abortError.name = "AbortError"
539+
throw abortError
540+
}
541+
525542
if (error instanceof vscode.CancellationError) {
526543
// The host rejected because the request was cancelled: either the
527544
// bridged external signal aborted or the user cancelled the request
@@ -647,24 +664,18 @@ export class VsCodeLmHandler extends BaseProvider implements SingleCompletionHan
647664
}
648665

649666
async completePrompt(prompt: string, options?: CompletePromptOptions): Promise<string> {
650-
// Fail fast if the caller already aborted before we even started: do not
651-
// initialize or invoke the host request for a cancelled request.
652-
if (options?.abortSignal?.aborted) {
653-
const abortError = new Error("VSCode LM completion aborted")
654-
abortError.name = "AbortError"
655-
throw abortError
656-
}
657-
658-
const client = await this.getClient()
659-
660667
// The VS Code LanguageModelChat API cannot carry an AbortSignal: sendRequest
661668
// only accepts a CancellationToken. Bridge the external signal and timeout
662-
// into a request-local CancellationTokenSource instead.
669+
// into a request-local CancellationTokenSource instead. Cancellation is
670+
// established before client initialization so the getClient() await is covered
671+
// by the timeout, the abort bridge, and the finally cleanup below.
663672
const tokenSource = new vscode.CancellationTokenSource()
664673
const externalAbortSignal = options?.abortSignal
665674

666675
// Apply the timeout only when it is a positive value: cancelling at once for a
667-
// zero/negative timeout would abort every such request immediately.
676+
// zero/negative timeout would abort every such request immediately. Starting
677+
// the timer before getClient() means a timeout that fires during a slow client
678+
// lookup still cancels the request before any sendRequest call.
668679
let timeoutId: ReturnType<typeof setTimeout> | undefined
669680
if (options?.timeoutMs !== undefined && options.timeoutMs > 0) {
670681
timeoutId = setTimeout(() => tokenSource.cancel(), options.timeoutMs)
@@ -688,9 +699,21 @@ export class VsCodeLmHandler extends BaseProvider implements SingleCompletionHan
688699
tokenSource.token.isCancellationRequested === true || externalAbortSignal?.aborted === true
689700

690701
try {
691-
// Re-check before invoking the host: the signal may have aborted while the
692-
// client was being initialized above.
693-
if (externalAbortSignal?.aborted) {
702+
// Fail fast if the caller already aborted before we even started: do not
703+
// initialize or invoke the host request for a cancelled request.
704+
if (isAborted()) {
705+
const abortError = new Error("VSCode LM completion aborted")
706+
abortError.name = "AbortError"
707+
throw abortError
708+
}
709+
710+
const client = await this.getClient()
711+
712+
// Re-check after client initialization: the signal may have aborted (or the
713+
// timeout fired) while getClient() was pending. Bail before invoking the host
714+
// so a timeout that fired during a slow client lookup can never lead to a
715+
// sendRequest call.
716+
if (isAborted()) {
694717
const abortError = new Error("VSCode LM completion aborted")
695718
abortError.name = "AbortError"
696719
throw abortError

0 commit comments

Comments
 (0)