Skip to content

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

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

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

Conversation

@AlemTuzlak
Copy link
Copy Markdown
Contributor

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

…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

Warning

Rate limit exceeded

@AlemTuzlak has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 5 minutes and 39 seconds before requesting another review.

Your organization is not enrolled in usage-based pricing. Contact your admin to enable usage-based pricing to continue reviews beyond the rate limit, or try again in 5 minutes and 39 seconds.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: ce9826e3-d992-4ea5-886f-5645667e6ddb

📥 Commits

Reviewing files that changed from the base of the PR and between 4afaa76 and 6d27f70.

📒 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
✨ Finishing Touches
🧪 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, 10 bumped as dependents.

🟩 Patch bumps

Package Version Reason
@tanstack/ai-client 0.7.7 → 0.7.8 Changeset
@tanstack/ai-preact 0.6.12 → 0.6.13 Dependent
@tanstack/ai-react 0.7.8 → 0.7.9 Dependent
@tanstack/ai-solid 0.6.12 → 0.6.13 Dependent
@tanstack/ai-svelte 0.6.12 → 0.6.13 Dependent
@tanstack/ai-vue 0.6.12 → 0.6.13 Dependent
@tanstack/ai-vue-ui 0.1.23 → 0.1.24 Dependent
@tanstack/smoke-tests-e2e 0.0.33 → 0.0.34 Dependent
ts-svelte-chat 0.1.27 → 0.1.28 Dependent
ts-vue-chat 0.1.27 → 0.1.28 Dependent
vanilla-chat 0.0.27 → 0.0.28 Dependent

@nx-cloud
Copy link
Copy Markdown

nx-cloud bot commented Apr 8, 2026

View your CI Pipeline Execution ↗ for commit 6d27f70

Command Status Duration Result
nx affected --targets=test:sherif,test:knip,tes... ✅ Succeeded 11s View ↗
nx run-many --targets=build --exclude=examples/** ✅ Succeeded 1s View ↗

☁️ Nx Cloud last updated this comment at 2026-04-08 11:53:13 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: 6d27f70

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