-
Notifications
You must be signed in to change notification settings - Fork 10.9k
fix(cli): race conditions in useGeminiStream parallel tool handling #17057
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
jw409
wants to merge
4
commits into
google-gemini:main
from
jw409:fix/usegeministream-race-conditions
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
b4be0c1
fix(cli): prevent stale closure in handleCompletedTools causing paral…
jw409 55af6ac
test(cli): add regression test for tool resubmission prevention
jw409 7e320e0
fix(cli): await submitQuery before marking tools as submitted
jw409 abcd634
test(cli): add regression test for submitQuery race condition
jw409 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jw408 why is this needed?
Can you explain...
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Happy Path (Works Fine)
t=0 Tool A and Tool B start
t=1 Tool A completes
→ handleCompletedTools([A]) fires
→ Submits A, marks A as submitted
→ React re-renders
→ useCallback recreates handleCompletedTools with fresh toolCalls
t=2 Tool B completes (after React re-rendered)
→ NEW handleCompletedTools([A, B]) fires
→ Callback sees toolCalls where A.responseSubmittedToGemini = true
→ Filters out A, only submits B ✓
Unhappy Path (Race Condition)
t=0 Tool A and Tool B start
t=1 Tool A completes
→ handleCompletedTools([A]) fires
→ Submits A, calls markToolsAsSubmitted(["A"])
→ State update is QUEUED (async)
t=1.001 Tool B completes BEFORE React re-renders
→ SAME OLD handleCompletedTools([A, B]) fires
→ This callback has a STALE CLOSURE - it captured toolCalls
from before A was marked as submitted
→ Callback sees A.responseSubmittedToGemini = false (stale!)
→ Submits A again 💥400 error
The Core Issue: Stale Closure
The useCallback captures toolCalls at creation time. If toolCalls wasn't in the dependency array (part of the fix), the callback never gets recreated with fresh state - it always sees the original toolCalls.
Even with toolCalls in deps, there's still a window between:
If another tool completes in that window, the old callback runs with stale state.
In practice on modern computers in unloaded state, the happy path happens the overwhelming majority of the time. If my system is swapping to death because I'm out of ram, or a ran a make -j 32 on an 8 core system and it's pegged, this could happen.
It's an edge case optimization for correctness, and I will readily admit I don't understand the nuances of the react ui loop and how it interacts with the various scheduling layers. I'm trying to help fix all these 400 parallel tool calling bugs, and this is one of the PR's I've split out from the original larger pr that was muddled, but in my mind they are all related at treating the same symptom.