fix(ai-ollama): close the request when the consumer stops a chat stream early #2311
main fromrefs/pull/2311/head·opened View on GitHub🎯 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 ollama SDK does not cancel the response body when iteration stops, so the adapter has to call response.abort() itself. It already did that in the finally of chatStream, but only inside if (signal), and signal is only set for an injected client with a caller signal. The default path (ollamaText() / createOllamaChat() without an abortController) 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:
- Unit: three cases in
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. - E2E:
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:build and build through nx affected, plus test:dts and the full Playwright suite (853 passed, 4 skipped). Not test:ai-review.
Not sure the early-stop abort belongs in the finally rather than in a wrapper around the SDK iterator. I kept it where the existing abort already was.
✅ Checklist#
- I have followed the steps in the Contributing guide.
- I have tested code changes locally with
pnpm run test:pr, or these tests do not apply to this pull request. (Thetest:prtargets above, withouttest:ai-review.) - I fully understand the code in this pull request, including any code generated with AI assistance.
- Docs: I updated
docs/for this change, or this change is not user-facing. - Changeset: I added a changeset (
pnpm changeset), or this PR does not change a published package.
🚀 Release Impact#
- This change affects published code, and I have generated a changeset.
- This change is docs/CI/dev-only (no release).
Summary by CodeRabbit#
- Bug Fixes
- Ollama chat requests now close when you stop reading a streamed response early, including when no caller-provided abort signal is used.
- Existing request cancellation signals are preserved.
- 1
tanstack-source added labelsbug
No actionable comments were generated in the recent review.
Walkthrough
-
The Ollama adapter now aborts the response when chat-stream iteration ends early, whether or not a caller signal exists. Unit tests and an end-to-end route test cover request closure.
Changes: Ollama stream cancellation
Layer / File(s) Summary Signal handling and early-stop cancellation
packages/ai-ollama/src/adapters/text.ts,packages/ai-ollama/tests/abort-signal-wire.test.ts,.changeset/ollama-early-stop-abort.mdclientForcombines the caller signal with an existing request signal.chatStreamremoves its abort listener and aborts the response when iteration ends early. Unit tests cover adapter-created clients with and without a caller signal, and injected clients without one.End-to-end request-close check
testing/e2e/src/routes/api.ollama-early-stop.ts,testing/e2e/src/routeTree.gen.ts,testing/e2e/tests/ollama-early-stop.spec.tsThe new POST route serves an unfinished response from a local Ollama stub and reports whether the request closes after the consumer stops reading. The generated route tree registers the route, and the end-to-end test checks the response. Estimated code review effort: 3 (Moderate) | ~20 minutes
Change: Bug fix
Possibly related PRs
- TanStack/ai#1975: Passes caller abort signals to provider requests, including Ollama stream cleanup, and changes the abort wiring tested by this PR.
Suggested reviewers:
alemtuzlak,arvavit
Merge Risk:
Minimal · up tob0d1c-
This change makes the Ollama adapter close its HTTP request when a consumer stops reading a chat stream early, with or without a caller abort signal. Unit tests and an end-to-end test cover the behavior. No actionable merge-blocking risk was found.
Pre-merge checks:
4-
Check name Status Explanation Title check The title names the main change: the adapter now closes the Ollama request when a consumer stops reading a chat stream early. It matches the changeset, the text.ts edits, the unit tests, and the E2E t… Description check The description follows the template. It explains the change and why it is needed, and it names the two edits in text.ts. It lists the unit and E2E tests with the results on main. It reports the check… Linked Issues check Check skipped because no linked issues were found for this pull request. Out of Scope Changes check Check skipped because no linked issues were found for this pull request. Full details: Title check
-
Explanation
The title names the main change: the adapter now closes the Ollama request when a consumer stops reading a chat stream early. It matches the changeset, the text.ts edits, the unit tests, and the E2E test. The 'fix(ai-ollama):' prefix is a conventional-commit scope and is acceptable.
Full details: Description check
-
Explanation
The description follows the template. It explains the change and why it is needed, and it names the two edits in text.ts. It lists the unit and E2E tests with the results on main. It reports the checks that were run and marks the checklist items. It also notes an open design question about where the abort belongs. The Docs item is checked with the claim that the change is not user-facing, and the Release Impact item is checked as a published change, which matches the changeset. The template's 'Changes' section is filled in, so the required information is present.
-
Finishing Touches
-
Generate unit tests (beta)
-
- Create a new PR
-
- Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Recent review info-
Run configuration-
- Source mode: Auto
- Repository: TanStack/ai
- Branch: main
- File: .coderabbit.yaml
- Commit: 840ea56
- Layers: Repository YAML
- Review profile: CHILL
- Plan: Advanced
- Run ID:
3e631994-fcd7-434a-aa2e-4af2566a73b9
Commits-
Reviewing files that changed from the base of the PR and between 840ea56621f4c32de4359849a0beaa343db60fe3 and b0d1cd4f694bcd5f3b7d3bdd335c0fe5e900d93e.
Files selected for processing: 6-
.changeset/ollama-early-stop-abort.mdpackages/ai-ollama/src/adapters/text.tspackages/ai-ollama/tests/abort-signal-wire.test.tstesting/e2e/src/routeTree.gen.tstesting/e2e/src/routes/api.ollama-early-stop.tstesting/e2e/tests/ollama-early-stop.spec.ts
Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.
-
-
github-actions[bot](GitHub profile, opens in a new tab) assignedtombeckenham
Thanks for the PR, @serhiizghama! 🙌 @tombeckenham will take a look.
Automated pre-review checks
- ✅ CI passing
- ✅ No merge conflicts
- ✅ Changeset present
- ✅ E2E test changes included
github-actions[bot](GitHub profile, opens in a new tab) added labelswaiting-on: maintainer
Sign in to join this conversation.
🦋 Changeset detected#
Latest commit: b0d1cd4f694bcd5f3b7d3bdd335c0fe5e900d93e
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