Skip to content

Commit 00249a6

Browse files
committed
fix(api): address CodeRabbit review on vscode-lm abort handling
- createMessage: sendRequest now uses the request-local cancellation source, the finally block disposes that local source and clears the shared field only when it still points at this request, and the error path no longer calls ensureCleanState (prevents an older finishing request from cancelling/disposing a newer request's token) - completePrompt: a pre-aborted signal now fails fast before getClient() and again before sendRequest(), so a cancelled request never initializes or invokes the host - completePrompt: a host vscode.CancellationError is normalized to an AbortError alongside isAborted() - spec: the timeout test now expects an AbortError (the cancelled token aborts the completion); the pre-abort test asserts sendRequest is never called; added a CancellationError -> AbortError rejection test; the mock CancellationTokenSource cancel() now flips isCancellationRequested to match the real API
1 parent c9753bf commit 00249a6

2 files changed

Lines changed: 58 additions & 32 deletions

File tree

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

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,17 @@ vi.mock("vscode", () => {
2626
})),
2727
},
2828
CancellationTokenSource: vi.fn(function () {
29+
// Faithful to the real API: cancel() marks the token as requested so
30+
// consumers that read isCancellationRequested observe the cancellation.
31+
const token = {
32+
isCancellationRequested: false,
33+
onCancellationRequested: vi.fn(),
34+
}
2935
return {
30-
token: {
31-
isCancellationRequested: false,
32-
onCancellationRequested: vi.fn(),
33-
},
34-
cancel: vi.fn(),
36+
token,
37+
cancel: vi.fn(() => {
38+
token.isCancellationRequested = true
39+
}),
3540
dispose: vi.fn(),
3641
}
3742
}),
@@ -1220,20 +1225,6 @@ describe("VsCodeLmHandler", () => {
12201225
})
12211226

12221227
it("should reject with an AbortError when the signal is already aborted", async () => {
1223-
const mockModel = { ...mockLanguageModelChat }
1224-
;(vscode.lm.selectChatModels as Mock).mockResolvedValueOnce([mockModel])
1225-
1226-
mockLanguageModelChat.sendRequest.mockResolvedValueOnce({
1227-
stream: (async function* () {
1228-
yield new vscode.LanguageModelTextPart("Completed text")
1229-
return
1230-
})(),
1231-
text: (async function* () {
1232-
yield "Completed text"
1233-
return
1234-
})(),
1235-
})
1236-
12371228
handler["client"] = mockLanguageModelChat
12381229

12391230
const controller = new AbortController()
@@ -1242,9 +1233,9 @@ describe("VsCodeLmHandler", () => {
12421233
const promise = handler.completePrompt("Test prompt", { abortSignal: controller.signal })
12431234
await expect(promise).rejects.toSatisfy((error) => error instanceof Error && error.name === "AbortError")
12441235

1245-
const tokenSource = tokenSourceInstance()
1246-
expect(tokenSource.cancel).toHaveBeenCalled()
1247-
expect(tokenSource.dispose).toHaveBeenCalled()
1236+
// Fails fast before invoking the host: no cancellation token source is
1237+
// created and the sendRequest is never called.
1238+
expect(mockLanguageModelChat.sendRequest).toHaveBeenCalledTimes(0)
12481239
})
12491240

12501241
it("should reject with an AbortError when the signal aborts mid-flight", async () => {
@@ -1309,10 +1300,12 @@ describe("VsCodeLmHandler", () => {
13091300
expect(tokenSource.cancel).toHaveBeenCalled()
13101301
expect(mockLanguageModelChat.sendRequest).toHaveBeenCalledTimes(1)
13111302

1312-
// The token-agnostic mock stream still completes, so the (unaborted-signal)
1313-
// completion resolves normally.
1303+
// The token-agnostic mock stream completes, but the cancelled token
1304+
// still makes the completion abort.
13141305
releaseStream()
1315-
await expect(promise).resolves.toBe("Completed text")
1306+
await expect(promise).rejects.toSatisfy(
1307+
(error) => error instanceof Error && error.name === "AbortError",
1308+
)
13161309
} finally {
13171310
vi.useRealTimers()
13181311
}
@@ -1361,6 +1354,19 @@ describe("VsCodeLmHandler", () => {
13611354
)
13621355
})
13631356
})
1357+
1358+
it("should reject with an AbortError when the host raises a CancellationError", async () => {
1359+
const mockModel = { ...mockLanguageModelChat }
1360+
;(vscode.lm.selectChatModels as Mock).mockResolvedValueOnce([mockModel])
1361+
1362+
// The host rejects with a CancellationError while the token flag is not set
1363+
// (no abort signal, no timeout), so only the error type signals the abort.
1364+
mockLanguageModelChat.sendRequest.mockRejectedValueOnce(new vscode.CancellationError())
1365+
handler["client"] = mockLanguageModelChat
1366+
1367+
const promise = handler.completePrompt("Test prompt")
1368+
await expect(promise).rejects.toSatisfy((error) => error instanceof Error && error.name === "AbortError")
1369+
})
13641370
})
13651371

13661372
describe("cleanMessageContent / deepClean", () => {

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

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ export class VsCodeLmHandler extends BaseProvider implements SingleCompletionHan
431431
const response: vscode.LanguageModelChatResponse = await client.sendRequest(
432432
vsCodeLmMessages,
433433
requestOptions,
434-
this.currentRequestCancellation.token,
434+
cancellationTokenSource.token,
435435
)
436436

437437
// Consume the stream and handle both text and tool call chunks
@@ -505,8 +505,6 @@ export class VsCodeLmHandler extends BaseProvider implements SingleCompletionHan
505505
outputTokens: totalOutputTokens,
506506
}
507507
} catch (error: unknown) {
508-
this.ensureCleanState()
509-
510508
if (error instanceof vscode.CancellationError) {
511509
// The host rejected because the request was cancelled: either the
512510
// bridged external signal aborted or the user cancelled the request
@@ -543,8 +541,12 @@ export class VsCodeLmHandler extends BaseProvider implements SingleCompletionHan
543541
if (onExternalAbort !== undefined) {
544542
externalAbortSignal?.removeEventListener("abort", onExternalAbort)
545543
}
546-
if (this.currentRequestCancellation) {
547-
this.currentRequestCancellation.dispose()
544+
// Dispose the request-local source. Clear the shared field only if it still
545+
// points at this request's source: a newer request may have replaced it, and
546+
// disposing the shared field here would cancel and dispose the newer request's
547+
// token.
548+
cancellationTokenSource.dispose()
549+
if (this.currentRequestCancellation === cancellationTokenSource) {
548550
this.currentRequestCancellation = null
549551
}
550552
}
@@ -628,6 +630,14 @@ export class VsCodeLmHandler extends BaseProvider implements SingleCompletionHan
628630
}
629631

630632
async completePrompt(prompt: string, options?: CompletePromptOptions): Promise<string> {
633+
// Fail fast if the caller already aborted before we even started: do not
634+
// initialize or invoke the host request for a cancelled request.
635+
if (options?.abortSignal?.aborted) {
636+
const abortError = new Error("VSCode LM completion aborted")
637+
abortError.name = "AbortError"
638+
throw abortError
639+
}
640+
631641
const client = await this.getClient()
632642

633643
// The VS Code LanguageModelChat API cannot carry an AbortSignal: sendRequest
@@ -661,6 +671,14 @@ export class VsCodeLmHandler extends BaseProvider implements SingleCompletionHan
661671
tokenSource.token.isCancellationRequested === true || externalAbortSignal?.aborted === true
662672

663673
try {
674+
// Re-check before invoking the host: the signal may have aborted while the
675+
// client was being initialized above.
676+
if (externalAbortSignal?.aborted) {
677+
const abortError = new Error("VSCode LM completion aborted")
678+
abortError.name = "AbortError"
679+
throw abortError
680+
}
681+
664682
const response = await client.sendRequest(
665683
[vscode.LanguageModelChatMessage.User(prompt)],
666684
{},
@@ -684,8 +702,10 @@ export class VsCodeLmHandler extends BaseProvider implements SingleCompletionHan
684702
} catch (error) {
685703
// Report an aborted request (external signal, timeout, or host
686704
// cancellation) as a standard AbortError instead of a generic
687-
// completion error.
688-
if (isAborted()) {
705+
// completion error. A host CancellationError is treated as a cancellation
706+
// even if the token flag was not observed (e.g. the host cancelled the
707+
// request through the token without the flag being set).
708+
if (isAborted() || error instanceof vscode.CancellationError) {
689709
const abortError = new Error("VSCode LM completion aborted")
690710
abortError.name = "AbortError"
691711
throw abortError

0 commit comments

Comments
 (0)