Skip to content

fix(ai-client): prevent drainPostStreamActions re-entrancy race condition#429

Open
AlemTuzlak wants to merge 5 commits intomainfrom
fix/drain-post-stream-reentrance
Open

fix(ai-client): prevent drainPostStreamActions re-entrancy race condition#429
AlemTuzlak wants to merge 5 commits intomainfrom
fix/drain-post-stream-reentrance

Conversation

@AlemTuzlak
Copy link
Copy Markdown
Contributor

@AlemTuzlak AlemTuzlak commented Apr 8, 2026

Summary

  • Add re-entrancy guard to drainPostStreamActions() preventing nested drain calls from stealing queued actions
  • Add shouldAutoSend() guard requiring at least one tool-call part before triggering auto-continuation (prevents spurious continuation after text-only responses)
  • When multiple client tools complete in the same round, each addToolResult() queues a checkForContinuation action. Without the guard, the first action's streamResponse()finallydrainPostStreamActions() (nested) steals remaining actions, permanently stalling the conversation

Fixes #302

Test plan

  • New test: "should continue after multiple client tools complete in the same round" — verifies two simultaneous tool calls trigger exactly one continuation and final text appears
  • All 199 @tanstack/ai-client tests pass

Summary by CodeRabbit

  • Bug Fixes
    • Fixed an issue where conversations could stall when processing multiple simultaneous tool calls and stream operations.
    • Enhanced automatic continuation logic to properly handle concurrent tool execution and prevent duplicate action processing.
    • Improved overall reliability of chat interactions involving complex, multi-step tool-based workflows.

…ueued actions

When multiple client tools complete in the same round, each addToolResult()
queues a checkForContinuation action. The first drain executes one action
which calls streamResponse(), whose finally block calls drainPostStreamActions()
again (nested). The inner drain steals the remaining actions, permanently
stalling the conversation.

Add a draining flag to skip nested drain calls. The outer drain processes
all actions sequentially, preventing action theft.

Also fix shouldAutoSend() to require at least one tool call in the last
assistant message. Previously it returned true for text-only responses
(areAllToolsComplete() returns true when toolParts.length === 0), causing
the second queued checkForContinuation action to incorrectly trigger an
extra continuation round and produce duplicate content.

Fixes #302
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 8, 2026

📝 Walkthrough

Walkthrough

This PR fixes a re-entrancy bug in drainPostStreamActions() where nested calls during stream finalization could consume actions meant for outer drain cycles, causing tool continuations to stall. The fix adds a draining guard flag and updates shouldAutoSend() to require tool-call parts before auto-sending, preventing spurious continuations on text-only responses.

Changes

Cohort / File(s) Summary
Changeset metadata
.changeset/fix-drain-post-stream-reentrance.md
Marks patch release for @tanstack/ai-client documenting the re-entrancy guard fix for drainPostStreamActions().
Implementation
packages/typescript/ai-client/src/chat-client.ts
Added draining boolean guard to prevent concurrent/nested drainPostStreamActions() invocations; updated shouldAutoSend() to verify the last assistant message exists and contains at least one tool-call part before auto-sending continuation, avoiding spurious sends on text-only responses.
Test coverage
packages/typescript/ai-client/tests/chat-client.test.ts
Added import of ConnectConnectionAdapter and new test case group validating drain re-entrancy behavior—simulates two-round stream with multiple simultaneous tool calls to ensure continuation text from round 2 is correctly processed.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Poem

🐰✨ A reentrancy guard stands watch at the door,
No nested drains shall steal actions anymore!
Tool calls now flow through their rightful round,
While rabbit-guarded queues stay safe and sound! 🛡️

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically describes the main change: adding a re-entrancy guard to drainPostStreamActions() to prevent a race condition that was causing chat stalls.
Description check ✅ Passed The description comprehensively covers the changes, root cause, test plan, and references the fixed issue #302, following the template's structure with clear sections.
Linked Issues check ✅ Passed The code changes implement Option B from issue #302: adds a draining flag to prevent nested drain calls [#302], adds shouldAutoSend() guard requiring tool-call parts [#302], and includes test verifying continuation after multiple simultaneous tool calls [#302].
Out of Scope Changes check ✅ Passed All changes directly address the linked issue #302: re-entrancy guard in drainPostStreamActions(), shouldAutoSend() enhancement, and test case for the specific scenario; no unrelated modifications detected.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/drain-post-stream-reentrance

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 8, 2026

🚀 Changeset Version Preview

1 package(s) bumped directly, 9 bumped as dependents.

🟩 Patch bumps

Package Version Reason
@tanstack/ai-client 0.7.8 → 0.7.9 Changeset
@tanstack/ai-preact 0.6.13 → 0.6.14 Dependent
@tanstack/ai-react 0.7.9 → 0.7.10 Dependent
@tanstack/ai-solid 0.6.13 → 0.6.14 Dependent
@tanstack/ai-svelte 0.6.13 → 0.6.14 Dependent
@tanstack/ai-vue 0.6.13 → 0.6.14 Dependent
@tanstack/ai-vue-ui 0.1.24 → 0.1.25 Dependent
ts-svelte-chat 0.1.29 → 0.1.30 Dependent
ts-vue-chat 0.1.29 → 0.1.30 Dependent
vanilla-chat 0.0.28 → 0.0.29 Dependent

@nx-cloud
Copy link
Copy Markdown

nx-cloud bot commented Apr 8, 2026

View your CI Pipeline Execution ↗ for commit 3d06f8b

Command Status Duration Result
nx run-many --targets=build --exclude=examples/** ✅ Succeeded 1m 15s View ↗

☁️ Nx Cloud last updated this comment at 2026-04-11 07:59:38 UTC

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new bot commented Apr 8, 2026

Open in StackBlitz

@tanstack/ai

npm i https://pkg.pr.new/@tanstack/ai@429

@tanstack/ai-anthropic

npm i https://pkg.pr.new/@tanstack/ai-anthropic@429

@tanstack/ai-client

npm i https://pkg.pr.new/@tanstack/ai-client@429

@tanstack/ai-code-mode

npm i https://pkg.pr.new/@tanstack/ai-code-mode@429

@tanstack/ai-code-mode-skills

npm i https://pkg.pr.new/@tanstack/ai-code-mode-skills@429

@tanstack/ai-devtools-core

npm i https://pkg.pr.new/@tanstack/ai-devtools-core@429

@tanstack/ai-elevenlabs

npm i https://pkg.pr.new/@tanstack/ai-elevenlabs@429

@tanstack/ai-event-client

npm i https://pkg.pr.new/@tanstack/ai-event-client@429

@tanstack/ai-fal

npm i https://pkg.pr.new/@tanstack/ai-fal@429

@tanstack/ai-gemini

npm i https://pkg.pr.new/@tanstack/ai-gemini@429

@tanstack/ai-grok

npm i https://pkg.pr.new/@tanstack/ai-grok@429

@tanstack/ai-groq

npm i https://pkg.pr.new/@tanstack/ai-groq@429

@tanstack/ai-isolate-cloudflare

npm i https://pkg.pr.new/@tanstack/ai-isolate-cloudflare@429

@tanstack/ai-isolate-node

npm i https://pkg.pr.new/@tanstack/ai-isolate-node@429

@tanstack/ai-isolate-quickjs

npm i https://pkg.pr.new/@tanstack/ai-isolate-quickjs@429

@tanstack/ai-ollama

npm i https://pkg.pr.new/@tanstack/ai-ollama@429

@tanstack/ai-openai

npm i https://pkg.pr.new/@tanstack/ai-openai@429

@tanstack/ai-openrouter

npm i https://pkg.pr.new/@tanstack/ai-openrouter@429

@tanstack/ai-preact

npm i https://pkg.pr.new/@tanstack/ai-preact@429

@tanstack/ai-react

npm i https://pkg.pr.new/@tanstack/ai-react@429

@tanstack/ai-react-ui

npm i https://pkg.pr.new/@tanstack/ai-react-ui@429

@tanstack/ai-solid

npm i https://pkg.pr.new/@tanstack/ai-solid@429

@tanstack/ai-solid-ui

npm i https://pkg.pr.new/@tanstack/ai-solid-ui@429

@tanstack/ai-svelte

npm i https://pkg.pr.new/@tanstack/ai-svelte@429

@tanstack/ai-vue

npm i https://pkg.pr.new/@tanstack/ai-vue@429

@tanstack/ai-vue-ui

npm i https://pkg.pr.new/@tanstack/ai-vue-ui@429

@tanstack/preact-ai-devtools

npm i https://pkg.pr.new/@tanstack/preact-ai-devtools@429

@tanstack/react-ai-devtools

npm i https://pkg.pr.new/@tanstack/react-ai-devtools@429

@tanstack/solid-ai-devtools

npm i https://pkg.pr.new/@tanstack/solid-ai-devtools@429

commit: 3d06f8b

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@packages/typescript/ai-client/tests/chat-client.test.ts`:
- Around line 11-14: The import specifiers in the import from
'../src/connection-adapters' are not alphabetized; reorder the named imports so
they are sorted alphabetically (e.g., place ConnectConnectionAdapter before
ConnectionAdapter) to satisfy the linter's sort-imports rule and update the
import line that currently lists ConnectionAdapter and ConnectConnectionAdapter.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 67771ad9-6029-4d21-a7c4-07a93e440236

📥 Commits

Reviewing files that changed from the base of the PR and between e8204b2 and 3d06f8b.

📒 Files selected for processing (3)
  • .changeset/fix-drain-post-stream-reentrance.md
  • packages/typescript/ai-client/src/chat-client.ts
  • packages/typescript/ai-client/tests/chat-client.test.ts

Comment on lines +11 to +14
import type {
ConnectionAdapter,
ConnectConnectionAdapter,
} from '../src/connection-adapters'
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Fix import member order to satisfy lint.

The import specifiers are not alphabetically sorted, which matches the reported sort-imports error.

🔧 Suggested fix
 import type {
-  ConnectionAdapter,
   ConnectConnectionAdapter,
+  ConnectionAdapter,
 } from '../src/connection-adapters'
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
import type {
ConnectionAdapter,
ConnectConnectionAdapter,
} from '../src/connection-adapters'
import type {
ConnectConnectionAdapter,
ConnectionAdapter,
} from '../src/connection-adapters'
🧰 Tools
🪛 ESLint

[error] 13-13: Member 'ConnectConnectionAdapter' of the import declaration should be sorted alphabetically.

(sort-imports)

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/typescript/ai-client/tests/chat-client.test.ts` around lines 11 -
14, The import specifiers in the import from '../src/connection-adapters' are
not alphabetized; reorder the named imports so they are sorted alphabetically
(e.g., place ConnectConnectionAdapter before ConnectionAdapter) to satisfy the
linter's sort-imports rule and update the import line that currently lists
ConnectionAdapter and ConnectConnectionAdapter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Client tool continuation stalls when multiple tools complete in the same round

1 participant