Repository navigation
fix(ai-ollama): close the request when the consumer stops a chat stream early - #2311
Conversation
🦋 Changeset detectedLatest commit: b0d1cd4 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
|
Thanks for the PR, @serhiizghama! 🙌 @tombeckenham will take a look. Automated pre-review checks
Automated triage — a human review follows. |
tombeckenham
left a comment
There was a problem hiding this comment.
Thanks for fixing the early-stop leak. I checked both hunks against the ollama SDK: response.abort() now runs when the consumer stops, AbortSignal.any keeps the SDK fetch signal so that abort still closes the socket, and the caller signal stays reusable. The unit and e2e coverage match that.
|
View your CI Pipeline Execution ↗ for commit b0d1cd4
💡 With Ultracache enabled, Nx Cloud learns each task's file inputs in CI and uses them on later runs. Turn on Ultracache in workspace settings ↗ · Read docs ↗ ☁️ Nx Cloud last updated this comment at |
@tanstack/ai
@tanstack/ai-acp
@tanstack/ai-angular
@tanstack/ai-anthropic
@tanstack/ai-bedrock
@tanstack/ai-byteplus
@tanstack/ai-cartesia
@tanstack/ai-claude-code
@tanstack/ai-client
@tanstack/ai-cloudflare
@tanstack/ai-code-mode
@tanstack/ai-code-mode-snippets
@tanstack/ai-codex
@tanstack/ai-cohere
@tanstack/ai-compaction
@tanstack/ai-computer-local
@tanstack/ai-computer-playwright
@tanstack/ai-computer-sandbox
@tanstack/ai-computer-use
@tanstack/ai-devtools-core
@tanstack/ai-durable-stream
@tanstack/ai-effect
@tanstack/ai-elevenlabs
@tanstack/ai-event-client
@tanstack/ai-fal
@tanstack/ai-gemini
@tanstack/ai-grok
@tanstack/ai-grok-build
@tanstack/ai-groq
@tanstack/ai-harness
@tanstack/ai-harness-cli
@tanstack/ai-isolate-cloudflare
@tanstack/ai-isolate-daytona
@tanstack/ai-isolate-e2b
@tanstack/ai-isolate-node
@tanstack/ai-isolate-quickjs
@tanstack/ai-isolate-quickjs-bun
@tanstack/ai-llmgateway
@tanstack/ai-lovable
@tanstack/ai-mcp
@tanstack/ai-mcp-sandbox
@tanstack/ai-memory
@tanstack/ai-meshy
@tanstack/ai-mistral
@tanstack/ai-muse
@tanstack/ai-neon
@tanstack/ai-octane
@tanstack/ai-ollama
@tanstack/ai-ollaya
@tanstack/ai-openai
@tanstack/ai-opencode
@tanstack/ai-openrouter
@tanstack/ai-optimize
@tanstack/ai-perplexity
@tanstack/ai-persistence
@tanstack/ai-preact
@tanstack/ai-react
@tanstack/ai-reactor
@tanstack/ai-remix
@tanstack/ai-sandbox
@tanstack/ai-sandbox-blaxel
@tanstack/ai-sandbox-boxd
@tanstack/ai-sandbox-cloudflare
@tanstack/ai-sandbox-daytona
@tanstack/ai-sandbox-docker
@tanstack/ai-sandbox-e2b
@tanstack/ai-sandbox-local-process
@tanstack/ai-sandbox-railway
@tanstack/ai-sandbox-sprites
@tanstack/ai-sandbox-upstash-box
@tanstack/ai-sandbox-vercel
@tanstack/ai-skills
@tanstack/ai-solid
@tanstack/ai-svelte
@tanstack/ai-tripo
@tanstack/ai-typesafe
@tanstack/ai-uthana
@tanstack/ai-utils
@tanstack/ai-vercel-compat
@tanstack/ai-vercel-gateway
@tanstack/ai-vertex
@tanstack/ai-vue
@tanstack/ai-worldlabs
@tanstack/ai-zai
@tanstack/openai-base
@tanstack/preact-ai-devtools
@tanstack/react-ai-devtools
@tanstack/solid-ai-devtools
@tanstack/svelte-ai-devtools
commit: |
🎯 Changes
If you break out of a streamed Ollama chat early, the HTTP request to Ollama stays open. The SDK's own
abort()works by closing that request, so while it stays open Ollama has no way to know that nobody is reading.The
ollamaSDK does not cancel the response body when iteration stops, so the adapter has to callresponse.abort()itself. It already did that in thefinallyofchatStream, but only insideif (signal), andsignalis only set for an injected client with a caller signal. The default path (ollamaText()/createOllamaChat()without anabortController) never got there.Two changes in
packages/ai-ollama/src/adapters/text.ts:chatStreamcallsresponse.abort()whenever the stream did not finish, with or without a caller signal. Removing the abort listener stays as it was.clientForused to replace the SDK's own fetch signal with the caller's signal. Thenresponse.abort()had nothing to cancel on the adapter's own client. It now combines both withAbortSignal.any, the same wayai-sandbox-railwaydoes. Aborting the caller signal still cancels the request, and stopping one call does not abort the caller signal thatchat()reuses for the next step.Tests:
abort-signal-wire.test.ts(own client without signal, own client with signal, injected client without signal). All three fail onmain. Reverting only theclientForchange fails the "own client with signal" case.ollama-early-stoprunschat()against a local NDJSON server that sends one chunk and keeps the response open, then breaks on the first text chunk. Onmainthe server still has the request open after 2 s. With this change it closes right away.I ran
test:sherif,test:knip,test:docs,test:kiira,test:maintainer,test:oxlint,test:lib,test:types,test:buildandbuildthroughnx affected, plustest:dtsand the full Playwright suite (853 passed, 4 skipped). Nottest:ai-review.Not sure the early-stop abort belongs in the
finallyrather than in a wrapper around the SDK iterator. I kept it where the existing abort already was.✅ Checklist
pnpm run test:pr, or these tests do not apply to this pull request. (Thetest:prtargets above, withouttest:ai-review.)docs/for this change, or this change is not user-facing.pnpm changeset), or this PR does not change a published package.🚀 Release Impact
Summary by CodeRabbit