Skip to content

Defensive coding to reduce the risk of Maximum update depth errors#20940

Merged
jacob314 merged 1 commit intomainfrom
fix_setstate
Mar 4, 2026
Merged

Defensive coding to reduce the risk of Maximum update depth errors#20940
jacob314 merged 1 commit intomainfrom
fix_setstate

Conversation

@jacob314
Copy link
Contributor

@jacob314 jacob314 commented Mar 3, 2026

Summary

Fixes #20939
with defensive coding. We're getting user reports from this in google3 but don't have a repro so resorting to defensive coding for this. Strangely no external users appear to be reporting it.

  • Prevented 'Maximum update depth exceeded' errors: Refactored the OverflowContext to implement a batched state update mechanism using useRef and setTimeout(0), effectively breaking synchronous update loops and preventing infinite render cycles that could lead to 'Maximum update depth exceeded' errors.
  • Improved useEffect management: Separated useEffect hooks in MaxSizedBox to better manage side effects, ensuring cleanup logic is distinct from state update logic, which contributes to more stable component behavior.
  • Enhanced test reliability: Updated MaxSizedBox.test.tsx to correctly handle asynchronous state updates in tests by integrating act and fake timers, ensuring that all React updates are flushed before assertions are made.

@jacob314 jacob314 requested a review from a team as a code owner March 3, 2026 08:07
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, 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 introduces defensive coding practices to resolve and prevent 'Maximum update depth exceeded' errors, particularly within the UI's overflow handling. The changes focus on stabilizing component rendering by batching state updates and refining side effect management, leading to a more robust and predictable user interface.

Highlights

  • Prevented 'Maximum update depth exceeded' errors: Refactored the OverflowContext to implement a batched state update mechanism using useRef and setTimeout(0), effectively breaking synchronous update loops and preventing infinite render cycles that could lead to 'Maximum update depth exceeded' errors.
  • Improved useEffect management: Separated useEffect hooks in MaxSizedBox to better manage side effects, ensuring cleanup logic is distinct from state update logic, which contributes to more stable component behavior.
  • Enhanced test reliability: Updated MaxSizedBox.test.tsx to correctly handle asynchronous state updates in tests by integrating act and fake timers, ensuring that all React updates are flushed before assertions are made.
Changelog
  • packages/cli/src/ui/components/shared/MaxSizedBox.test.tsx
    • Added act and fake timer utilities (vi.useFakeTimers, vi.useRealTimers) to tests.
    • Wrapped asynchronous operations in act to ensure proper React test environment behavior and flush pending updates.
  • packages/cli/src/ui/components/shared/MaxSizedBox.tsx
    • Refactored useEffect hooks to separate the cleanup logic into its own effect, improving side effect management.
  • packages/cli/src/ui/contexts/OverflowContext.tsx
    • Introduced useRef for idsRef and timeoutRef to manage overflowing IDs and batch state updates.
    • Implemented a syncState function that uses setTimeout(0) to defer state updates, preventing synchronous recursive updates.
    • Modified addOverflowingId, removeOverflowingId, and reset functions to update idsRef and trigger batched state synchronization.
    • Added an useEffect to clear the timeout on component unmount.
Activity
  • No specific activity has been recorded for this pull request yet.
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.

@google-gemini google-gemini deleted a comment from gemini-cli bot Mar 3, 2026
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 introduces defensive coding to prevent "Maximum update depth exceeded" errors in React, which can occur from layout-dependent state changes. The main change in OverflowContext.tsx implements a state update batching mechanism using setTimeout to break synchronous render loops, which is a robust way to handle layout oscillation. The related modifications in MaxSizedBox.tsx and its test file are appropriate and necessary to support this new asynchronous behavior. Overall, the changes are well-implemented and improve the stability of the UI components.

@github-actions
Copy link

github-actions bot commented Mar 3, 2026

Size Change: +682 B (0%)

Total Size: 25.8 MB

ℹ️ View Unchanged
Filename Size Change
./bundle/gemini.js 25.3 MB +682 B (0%)
./bundle/node_modules/@google/gemini-cli-devtools/dist/client/main.js 221 kB 0 B
./bundle/node_modules/@google/gemini-cli-devtools/dist/src/_client-assets.js 227 kB 0 B
./bundle/node_modules/@google/gemini-cli-devtools/dist/src/index.js 11.5 kB 0 B
./bundle/node_modules/@google/gemini-cli-devtools/dist/src/types.js 132 B 0 B
./bundle/sandbox-macos-permissive-open.sb 890 B 0 B
./bundle/sandbox-macos-permissive-proxied.sb 1.31 kB 0 B
./bundle/sandbox-macos-restrictive-open.sb 3.36 kB 0 B
./bundle/sandbox-macos-restrictive-proxied.sb 3.56 kB 0 B
./bundle/sandbox-macos-strict-open.sb 4.82 kB 0 B
./bundle/sandbox-macos-strict-proxied.sb 5.02 kB 0 B

compressed-size-action

newIds.add(id);
return newIds;
});
/**
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is acceptable as there is no need to immediately show the UI that boxes are overflowing. Showing the UI a couple ms or even 50 ms later would be just as good.

@jacob314 jacob314 added this pull request to the merge queue Mar 4, 2026
Merged via the queue into main with commit 1017b78 Mar 4, 2026
27 checks passed
@jacob314 jacob314 deleted the fix_setstate branch March 4, 2026 06:44
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.

Maximum update depth exceeded that appear to be related to OverflowContext.tsx

2 participants