Skip to content

Commit cb406ad

Browse files
committed
fix(api): address CodeRabbit review on native-ollama abort handling
1 parent 5bcb16f commit cb406ad

2 files changed

Lines changed: 57 additions & 13 deletions

File tree

‎src/api/providers/__tests__/native-ollama.spec.ts‎

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,20 @@ describe("NativeOllamaHandler", () => {
823823
it("should call client.abort() when timeoutMs is reached", async () => {
824824
const testTimeout = 5000
825825
let capturedFn: (() => void) | undefined
826+
let capturedInstanceAbort: (() => void) | undefined
827+
828+
// Capture the per-request client's abort spy so the assertion targets
829+
// the actual abort() call rather than just the constructor call.
830+
OllamaMock.mockImplementation(function (options?: { host?: string }) {
831+
const instanceAbort = vitest.fn()
832+
capturedInstanceAbort = instanceAbort
833+
return {
834+
chat: mockChat,
835+
abort: instanceAbort,
836+
_host: options?.host ?? "http://localhost:11434",
837+
_instanceAbort: instanceAbort,
838+
}
839+
})
826840

827841
// The timer id is never consumed (the callback is captured and fired
828842
// manually), so bridge the ambient setTimeout signature through unknown.
@@ -842,7 +856,8 @@ describe("NativeOllamaHandler", () => {
842856
expect(capturedFn).toBeDefined()
843857
if (capturedFn) capturedFn()
844858
// The timeout callback should have invoked client.abort() on the request-local instance
845-
expect(OllamaMock).toHaveBeenCalled()
859+
expect(capturedInstanceAbort).toBeDefined()
860+
expect(capturedInstanceAbort).toHaveBeenCalledTimes(1)
846861
})
847862

848863
it("should call instance.abort() when abortSignal is aborted", async () => {
@@ -902,7 +917,22 @@ describe("NativeOllamaHandler", () => {
902917
expect(capturedInstanceAbort).toHaveBeenCalledTimes(1)
903918
})
904919

905-
it("should not create a request-local client or timer for non-positive timeoutMs", async () => {
920+
it("should reject with AbortError without fetching models when abortSignal is already aborted", async () => {
921+
const controller = new AbortController()
922+
controller.abort()
923+
924+
await expect(
925+
handler.completePrompt("Test prompt", { abortSignal: controller.signal }),
926+
).rejects.toMatchObject({
927+
name: "AbortError",
928+
})
929+
930+
// The pre-aborted check must short-circuit before any network call.
931+
expect(mockGetOllamaModels).not.toHaveBeenCalled()
932+
expect(mockChat).not.toHaveBeenCalled()
933+
})
934+
935+
it("should not start a timeout timer for non-positive timeoutMs", async () => {
906936
const setTimeoutSpy = vitest.spyOn(global, "setTimeout")
907937
mockChat.mockResolvedValue({
908938
message: { content: "Response" },

‎src/api/providers/native-ollama.ts‎

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -552,12 +552,12 @@ export class NativeOllamaHandler extends BaseProvider implements SingleCompletio
552552

553553
console.error(`Ollama API error (${statusCode || "unknown"}): ${errorMessage}`)
554554
throw error
555-
}
556-
557-
// The request completed normally (the error path above always rethrows);
558-
// detach the external-signal listener so it cannot outlive this request.
559-
if (onExternalAbort) {
560-
externalAbortSignal?.removeEventListener("abort", onExternalAbort)
555+
} finally {
556+
// Detach the external-signal listener so it cannot outlive this request,
557+
// including on error paths and early generator finalization.
558+
if (onExternalAbort) {
559+
externalAbortSignal?.removeEventListener("abort", onExternalAbort)
560+
}
561561
}
562562
}
563563

@@ -584,15 +584,16 @@ export class NativeOllamaHandler extends BaseProvider implements SingleCompletio
584584
let onAbort: (() => void) | undefined
585585

586586
try {
587-
const { id: modelId } = await this.fetchModel()
588-
const useR1Format = modelId.toLowerCase().includes("deepseek-r1")
589-
590-
// Handle timeoutMs if provided
587+
// Handle timeoutMs if provided (client already exists above, so the
588+
// timer can abort the per-request client directly)
591589
if (options?.timeoutMs !== undefined && options.timeoutMs > 0) {
592590
timeoutId = setTimeout(() => client.abort(), options.timeoutMs)
593591
}
594592

595-
// Propagate abortSignal into the per-request client via client.abort()
593+
// Propagate abortSignal into the per-request client via client.abort().
594+
// Set this up before fetchModel() so an already-aborted signal (or one
595+
// that aborts during the model-list fetch) is honored before the
596+
// request proceeds.
596597
if (options?.abortSignal) {
597598
if (options.abortSignal.aborted) {
598599
client.abort()
@@ -610,6 +611,19 @@ export class NativeOllamaHandler extends BaseProvider implements SingleCompletio
610611
}
611612
}
612613

614+
const { id: modelId } = await this.fetchModel()
615+
616+
// Re-check after the model-list fetch: if the signal aborted during
617+
// fetchModel(), the listener above already aborted the client, and the
618+
// request must reject instead of proceeding to chat().
619+
if (options?.abortSignal?.aborted) {
620+
const abortError = new Error("This operation was aborted")
621+
abortError.name = "AbortError"
622+
throw abortError
623+
}
624+
625+
const useR1Format = modelId.toLowerCase().includes("deepseek-r1")
626+
613627
// Reuse the shared request-option builder so single-shot
614628
// completions respect the same reasoning configuration as the
615629
// streaming path.

0 commit comments

Comments
 (0)