Skip to content

fix(ui): guard word-wrap while-loop against infinite loop on wide/oversized characters#25432

Closed
AKIB473 wants to merge 2 commits intogoogle-gemini:mainfrom
AKIB473:akib/fix-word-wrap-infinite-loop
Closed

fix(ui): guard word-wrap while-loop against infinite loop on wide/oversized characters#25432
AKIB473 wants to merge 2 commits intogoogle-gemini:mainfrom
AKIB473:akib/fix-word-wrap-infinite-loop

Conversation

@AKIB473
Copy link
Copy Markdown

@AKIB473 AKIB473 commented Apr 15, 2026

Fixes #19985

Root cause

In InputPrompt.tsx, the word-wrap loop that splits oversized words across lines:

while (stringWidth(wordToProcess) > inputWidth) {
  let splitIndex = 0;
  for (let i = 0; i < wordCP.length; i++) {
    const charWidth = stringWidth(char);
    if (partWidth + charWidth > inputWidth) break;   // ← breaks with splitIndex=0
    splitIndex = i + 1;
  }
  additionalLines.push(part);
  wordToProcess = cpSlice(wordToProcess, splitIndex); // ← cpSlice(word, 0) = word unchanged
}

When a single codepoint is wider than inputWidth (e.g. a CJK character in a very narrow terminal, or any wide Unicode character), the for-loop breaks on the first iteration with splitIndex = 0. cpSlice(wordToProcess, 0) returns the entire word unchanged, so wordToProcess never shrinks and the while-loop spins forever, hanging the CLI.

Fix

After the for-loop, if splitIndex === 0, advance it to 1 and set part to the first codepoint:

// Guard against infinite loop: if no codepoint fit (e.g. a single wide
// character exceeds inputWidth), advance by one codepoint so the loop
// always terminates.
if (splitIndex === 0) {
  splitIndex = 1;
  part = cpSlice(wordToProcess, 0, 1);
}

This mirrors the pattern used by cpSlice(line, col - 1, col) already in the file, and guarantees the loop always makes progress.

Tests

Two regression tests added in InputPrompt.test.tsx:

  • CJK character (display width 2) into a 1-column terminal — was infinite loop before fix
  • Long ASCII word into a 3-column terminal — exercises the normal wrap path

Checklist

  • I've read the CONTRIBUTING guide
  • Existing tests pass
  • New regression tests added
  • One-line bug-fix, no behaviour change for normal inputs

AKIB473 added 2 commits April 15, 2026 09:50
…racters

When a single codepoint is wider than inputWidth (e.g. a CJK character
or emoji in a very narrow terminal), the inner for-loop breaks
immediately with splitIndex=0. The subsequent cpSlice(wordToProcess, 0)
then returns the entire word unchanged, causing the while-loop to spin
forever and hang the CLI.

Fix: after the for-loop, if splitIndex is still 0, force it to 1 and
set part to the first codepoint. This guarantees the loop always
advances by at least one codepoint per iteration.

Fixes google-gemini#19985
Two regression tests:
1. Wide CJK character (width=2) in a 1-column terminal — previously hung
2. Long ASCII word in a 3-column terminal — exercises the wrap loop

Related to fix for google-gemini#19985
@AKIB473 AKIB473 requested a review from a team as a code owner April 15, 2026 03:50
@google-cla
Copy link
Copy Markdown

google-cla Bot commented Apr 15, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@gemini-code-assist
Copy link
Copy Markdown
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 addresses a critical issue where the CLI would hang indefinitely when encountering wide characters that exceed the available input width. By ensuring the word-wrapping loop always makes progress even when a single character is wider than the allowed space, the fix guarantees terminal stability under various input conditions.

Highlights

  • Infinite Loop Prevention: Added a guard clause in the word-wrap logic to ensure that if no characters fit within the input width, the loop advances by at least one codepoint to prevent hanging.
  • Regression Testing: Introduced new test cases in InputPrompt.test.tsx to verify behavior with wide characters (like CJK) and long words in narrow terminal environments.
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.

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
Copy Markdown
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 a guard in the word-wrapping logic within InputPrompt.tsx to prevent infinite loops when processing wide characters that exceed the input width. Regression tests were added to verify this fix. However, the current test implementation is ineffective because it does not trigger the necessary React re-renders to exercise the ghost text logic, and the reviewer suggests adopting the TestWrapper pattern to ensure the tests accurately reflect component behavior.

Comment on lines +2422 to +2450
describe('word-wrap infinite loop guard (issue #19985)', () => {
it('should not hang when a wide CJK character exceeds inputWidth', async () => {
// Regression test: when a single CJK/emoji character is wider than inputWidth,
// the word-wrap while-loop previously spun forever because splitIndex stayed 0
// and cpSlice(word, 0) returned the full word unchanged.
// The fix ensures splitIndex advances by at least 1 codepoint each iteration.
const narrowProps = { ...props, inputWidth: 1 };
const { stdin, unmount } = await renderWithProviders(
<TestInputPrompt {...narrowProps} />,
{ isTTY: true },
);
// Type a CJK character (width=2) into a terminal that is only 1 column wide.
// Without the fix this blocks indefinitely; with the fix it completes promptly.
await stdin.write('中');
// If we reach here the loop did not hang — test passes.
unmount();
});

it('should not hang when a long ASCII word exceeds inputWidth', async () => {
const narrowProps = { ...props, inputWidth: 3 };
const { stdin, unmount } = await renderWithProviders(
<TestInputPrompt {...narrowProps} />,
{ isTTY: true },
);
// "hello" is 5 chars wide but inputWidth is 3 — triggers the wrap loop.
await stdin.write('hello');
unmount();
});
});
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.

high

The regression tests added here do not appear to exercise the code path containing the fix. getGhostTextLines returns early if completion.promptCompletion.text is empty (as configured in the beforeEach mock) or if it doesn't start with buffer.text. Furthermore, mutating mockBuffer.text directly via stdin.write does not trigger a React re-render in TestInputPrompt, meaning the ghost text logic is never re-evaluated with the new input. To effectively test this, consider using the TestWrapper pattern (see line 2459) which uses state to trigger re-renders, and ensure the mock completion text is set to a value that triggers the wrapping logic. When updating these tests, ensure that any use of renderWithProviders includes a call to unmount at the end to prevent resource leaks. Additionally, since getGhostTextLines involves complex layout logic, ensure the implementation contains detailed comments explaining the height derivations.

References
  1. When using renderWithProviders in tests, the returned unmount function must be called at the end of the test to ensure proper cleanup and prevent resource leaks.
  2. For complex layout calculations that depend on component rendering logic (like conditional borders or padding), add detailed comments explaining how the height is derived to prevent incorrect refactoring.

@gemini-cli gemini-cli Bot added priority/p2 Important but can be addressed in a future release. area/core Issues related to User Interface, OS Support, Core Functionality help wanted We will accept PRs from all issues marked as "help wanted". Thanks for your support! labels Apr 15, 2026
@gundermanc
Copy link
Copy Markdown
Member

This PR hasn't been updated in 7 days. Is it still active?

@spencer426
Copy link
Copy Markdown
Contributor

Thank you for your interest in contributing to the project! We are closing this PR due to inactivity.

@spencer426 spencer426 closed this Apr 28, 2026
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 help wanted We will accept PRs from all issues marked as "help wanted". Thanks for your support! priority/p2 Important but can be addressed in a future release.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CLI hangs/freezes when using @filename:line or @filename:range syntax

3 participants