Skip to content

Conversation

@jw409
Copy link
Contributor

@jw409 jw409 commented Jan 20, 2026

Fixes #17071

Adds isRespondingRef to provide synchronous blocking of concurrent
queries. React state updates are batched, so checking streamingState
alone wasn't sufficient - the loop retry's submitQuery could race
with user prompts because the state update wasn't visible immediately.

Changes:
- Add isRespondingRef mirror of isResponding state for synchronous checks
- Update guard at line 949 to check isRespondingRef.current
- Set isRespondingRef.current = true/false alongside setIsResponding calls
- Make loop detection onComplete callback async and await submitQuery
- Add regression test that verifies user prompts are blocked during retry

The test demonstrates the race condition by:
1. Triggering loop detection
2. Making the retry slow (controlled promise)
3. User selecting "disable" (triggers retry)
4. Attempting to send user prompt while retry is in progress
5. Verifying retry finishes before user prompt starts

References: google-gemini#16144, google-gemini#16220
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @jw409, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request resolves a critical race condition in the CLI's query submission logic. Previously, when a user responded to a loop detection prompt by disabling it, the subsequent retry query could race with new user input due to the asynchronous nature of React state updates. The fix introduces a synchronous reference to the isResponding state and ensures that retry queries are awaited, thereby preventing concurrent requests and maintaining proper query sequencing.

Highlights

  • Race Condition Fix: Addresses a race condition where user prompts could interfere with the loop detection retry mechanism, particularly when the user disables loop detection.
  • Synchronous State Management: Introduces isRespondingRef (a useRef hook) to provide a synchronous mirror of the isResponding state. This circumvents React's state batching, allowing for immediate checks of the system's response status.
  • Query Guard Enhancement: The submitQuery function's guard condition has been updated to check isRespondingRef.current in addition to streamingState, ensuring that new queries are properly blocked while a retry is in progress.
  • Asynchronous Retry Handling: The onComplete callback for loop detection confirmation is now async, and the submitQuery call for retrying the original query is awaited. This guarantees that the retry completes before any subsequent user prompts can be processed.
  • Regression Test: A new regression test has been added to specifically validate the fix. This test simulates a slow retry and a concurrent user prompt, asserting that the user prompt is correctly blocked until the retry operation finishes.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request addresses a race condition in the loop detection retry mechanism. The fix introduces a useRef (isRespondingRef) to synchronously track the responding state, preventing new user queries from starting while a retry is in progress. The onComplete handler for loop detection is also made async to properly await the retry query. The changes are well-implemented and are accompanied by a thorough regression test that validates the fix. The code looks good, and I have no further comments.

@gemini-cli gemini-cli bot added the area/core Issues related to User Interface, OS Support, Core Functionality label Jan 20, 2026
@jw409 jw409 marked this pull request as ready for review January 20, 2026 03:16
@jw409 jw409 requested a review from a team as a code owner January 20, 2026 03:16
@jw409
Copy link
Contributor Author

jw409 commented Jan 20, 2026

Fixes #17071

@jw409
Copy link
Contributor Author

jw409 commented Jan 20, 2026

✅ Preflight verified locally: 4091 tests pass, 1 known flaky test (MCP OAuth timeout - tracked separately).

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

Labels

area/core Issues related to User Interface, OS Support, Core Functionality

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: loop retry race condition when user clicks disable

1 participant