From 8e7b1c6884286b8d8a0985de8d46a8a03cfaa259 Mon Sep 17 00:00:00 2001 From: Robin Newhouse Date: Thu, 4 Dec 2025 13:56:25 -0800 Subject: [PATCH] fix(ollama): abort streaming requests when task is cancelled (#7907) Previously, clicking cancel would break out of the stream loop but leave the HTTP connection open. Ollama would continue generating in the background, keeping the GPU busy and blocking subsequent requests until completion. Now we call the Ollama SDK's abort() method to immediately close the connection. Fixes #7468 --- .changeset/whole-books-hear.md | 5 +++++ src/core/api/index.ts | 1 + src/core/api/providers/ollama.ts | 4 ++++ src/core/task/index.ts | 1 + 4 files changed, 11 insertions(+) create mode 100644 .changeset/whole-books-hear.md diff --git a/.changeset/whole-books-hear.md b/.changeset/whole-books-hear.md new file mode 100644 index 00000000..b04b5dea --- /dev/null +++ b/.changeset/whole-books-hear.md @@ -0,0 +1,5 @@ +--- +"claude-dev": patch +--- + +fix(ollama): abort streaming requests when task is cancelled diff --git a/src/core/api/index.ts b/src/core/api/index.ts index a583ed54..fbc86c5f 100644 --- a/src/core/api/index.ts +++ b/src/core/api/index.ts @@ -51,6 +51,7 @@ export interface ApiHandler { createMessage(systemPrompt: string, messages: ClineStorageMessage[], tools?: ClineTool[], useResponseApi?: boolean): ApiStream getModel(): ApiHandlerModel getApiStreamUsage?(): Promise + abort?(): void } export interface ApiHandlerModel { diff --git a/src/core/api/providers/ollama.ts b/src/core/api/providers/ollama.ts index 29f94130..3f093878 100644 --- a/src/core/api/providers/ollama.ts +++ b/src/core/api/providers/ollama.ts @@ -121,4 +121,8 @@ export class OllamaHandler implements ApiHandler { }, } } + + abort(): void { + this.client?.abort() + } } diff --git a/src/core/task/index.ts b/src/core/task/index.ts index 6f0fa084..9ecf4235 100644 --- a/src/core/task/index.ts +++ b/src/core/task/index.ts @@ -2907,6 +2907,7 @@ export class Task { ) if (this.taskState.abort) { + this.api.abort?.() if (!this.taskState.abandoned) { // only need to gracefully abort if this instance isn't abandoned (sometimes openrouter stream hangs, in which case this would affect future instances of cline) await abortStream("user_cancelled")