Skip to content

Commit 2df5449

Browse files
committed
fix(api): release the completion handler when cancellation outlives model selection
1 parent 9910985 commit 2df5449

3 files changed

Lines changed: 48 additions & 4 deletions

File tree

‎src/api/providers/__tests__/kimi-code.spec.ts‎

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,11 @@ describe("KimiCodeHandler", () => {
145145

146146
await expect(handler.completePrompt("test", { abortSignal: signal, timeoutMs: 5_000 })).resolves.toBe("retried")
147147

148-
// Both the initial attempt and the retry after the forced refresh carry the options.
149-
expect(createCompletion).toHaveBeenNthCalledWith(1, expect.anything(), { signal, timeout: 5_000 })
150-
expect(createCompletion).toHaveBeenNthCalledWith(2, expect.anything(), { signal, timeout: 5_000 })
148+
// Both the initial attempt and the retry after the forced refresh carry the caller's own signal.
149+
expect(createCompletion.mock.calls[0][1].signal).toBe(signal)
150+
expect(createCompletion.mock.calls[0][1].timeout).toBe(5_000)
151+
expect(createCompletion.mock.calls[1][1].signal).toBe(signal)
152+
expect(createCompletion.mock.calls[1][1].timeout).toBe(5_000)
151153
})
152154

153155
it("does not start preparation when the caller already aborted", async () => {
@@ -159,6 +161,26 @@ describe("KimiCodeHandler", () => {
159161
expect(mockGetAccessToken).not.toHaveBeenCalled()
160162
})
161163

164+
it("stops before model discovery and completion when aborted during token resolution", async () => {
165+
const handler = new KimiCodeHandler({ kimiCodeAuthMethod: "oauth" })
166+
let resolveToken!: (token: string) => void
167+
mockGetAccessToken.mockReturnValueOnce(
168+
new Promise<string>((resolve) => {
169+
resolveToken = resolve
170+
}),
171+
)
172+
const controller = new AbortController()
173+
174+
const completion = handler.completePrompt("test", { abortSignal: controller.signal })
175+
await vi.waitFor(() => expect(resolveToken).toBeDefined())
176+
controller.abort()
177+
resolveToken("oauth-token")
178+
179+
await expect(completion).rejects.toMatchObject({ name: "AbortError" })
180+
// The cancelled request never reaches model discovery or the provider.
181+
expect(mockGetModels).not.toHaveBeenCalled()
182+
})
183+
162184
it("does not retry on 401 when using API key auth", async () => {
163185
const handler = new KimiCodeHandler({ kimiCodeAuthMethod: "api-key", kimiCodeApiKey: "key" })
164186
vi.spyOn(globalThis, "fetch").mockResolvedValueOnce(new Response(null, { status: 401 }))

‎src/utils/__tests__/enhance-prompt.spec.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,23 @@ describe("enhancePrompt", () => {
118118
expect(succeeding.dispose).toHaveBeenCalledOnce()
119119
})
120120

121+
it("settles on abort and disposes the handler while the provider request is still pending", async () => {
122+
// Model discovery or provider preparation may never settle; the caller's stop must still
123+
// release the handler and its listeners.
124+
const pendingHandler = createCompletionHandlerDouble({
125+
completePrompt: vi.fn(() => new Promise<string>(() => {})),
126+
dispose: vi.fn(),
127+
})
128+
vi.mocked(buildApiHandler).mockReturnValueOnce(pendingHandler)
129+
const abortController = new AbortController()
130+
131+
const completion = singleCompletionHandler(mockApiConfig, "a", { abortSignal: abortController.signal })
132+
abortController.abort()
133+
134+
await expect(completion).rejects.toMatchObject({ name: "AbortError" })
135+
expect(pendingHandler.dispose).toHaveBeenCalledOnce()
136+
})
137+
121138
it("disposes the handler when the provider does not support prompt enhancement", async () => {
122139
const unsupported = createApiHandlerDouble({ dispose: vi.fn() })
123140
vi.mocked(buildApiHandler).mockReturnValueOnce(unsupported)

‎src/utils/single-completion-handler.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { ProviderSettings } from "@roo-code/types"
22

33
import { buildApiHandler, disposeApiHandler, SingleCompletionHandler, type CompletePromptOptions } from "../api"
4+
import { settleOnAbort } from "../api/providers/utils/abort-signal"
45

56
/**
67
* Enhances a prompt using the configured API without creating a full Cline instance or task history.
@@ -26,7 +27,11 @@ export async function singleCompletionHandler(
2627
throw new Error("The selected API provider does not support prompt enhancement")
2728
}
2829

29-
return await (handler as SingleCompletionHandler).completePrompt(promptText, options)
30+
const completion = (handler as SingleCompletionHandler).completePrompt(promptText, options)
31+
// Settle on the caller's stop even when the provider never returns (model discovery or a
32+
// preparation step can outlive it), so the handler below is disposed and its listeners are
33+
// released instead of being retained by a request nobody waits for any more.
34+
return await settleOnAbort(completion, options?.abortSignal, apiConfiguration.apiProvider)
3035
} finally {
3136
// Handlers such as VS Code LM / GitHub Copilot hold listeners that must be released.
3237
disposeApiHandler(handler)

0 commit comments

Comments
 (0)