Skip to content

fix(core): "gemini --resume crash" handle EBADF error when PTY fd is stale on session resume - #27371

Closed
mini2s wants to merge 4 commits into
google-gemini:mainfrom
mini2s:fix-resume-crushed
Closed

fix(core): "gemini --resume crash" handle EBADF error when PTY fd is stale on session resume #27371
mini2s wants to merge 4 commits into
google-gemini:mainfrom
mini2s:fix-resume-crushed

Conversation

@mini2s

@mini2s mini2s commented May 22, 2026

Copy link
Copy Markdown
Contributor

Summary

Fix ioctl(2) failed, EBADF crash when using gemini --resume due to stale PTY file descriptors.

Details

Add EBADF (bad file descriptor) to the error codes that are safely ignored in ShellExecutionService.resizePty()'s catch block.

Background: When resuming a session, the PTY fd from the previous session has already been closed (the process exited). However, ExecutionLifecycleService.isActive() still returns true because resume re-attaches the execution. The resize call invokes ioctl(fd, TIOCSWINSZ, ...) on the stale fd and gets EBADF instead of ESRCH, causing an unhandled exception that crashes the process.

Fix: Treat EBADF the same as the already-handled ESRCH and Windows PTY exit errors — silently ignore it.

Files changed:

  • packages/core/src/services/shellExecutionService.ts — add EBADF check in the resizePty catch block (+3 lines)

Related Issues

#27327

How to Validate

  1. Start an interactive gemini session and run a command that spawns a background task
  2. Exit the session with exit or Ctrl+C
  3. Run gemini --resume to restore the session
  4. Confirm the ioctl(2) failed, EBADF error no longer occurs

Unit tests:

npm test -w @google/gemini-cli-core -- src/services/shellExecutionService.test.ts

All 27 PTY-related tests pass.

Pre-Merge Checklist

  • Updated relevant documentation and README (if needed)
  • Added/updated tests (if needed) — existing tests provide coverage
  • Noted breaking changes (if any)
  • Validated on required platforms/methods:
    • MacOS
      • npm run
      • npx
      • Docker
      • Podman
      • Seatbelt
    • Windows
      • npm run
      • npx
      • Docker
    • Linux
      • npm run
      • npx
      • Docker

@mini2s
mini2s requested a review from a team as a code owner May 22, 2026 10:06
@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 crash occurring during gemini session resumption. When a session is resumed, the system occasionally attempts to resize a PTY using a stale file descriptor, resulting in an EBADF error. By explicitly catching and ignoring this error, the service now gracefully handles the state mismatch, improving overall application stability.

Highlights

  • Error Handling Update: Added EBADF to the list of ignored error codes in ShellExecutionService.resizePty to prevent crashes during session resumption.
  • Stability Improvement: Ensured that stale PTY file descriptors do not trigger unhandled exceptions when attempting to resize the terminal window upon resuming a session.
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 the 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 counterproductive. 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.

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

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.

Code Review

This pull request updates the ShellExecutionService to ignore EBADF (bad file descriptor) errors when attempting to resize a PTY. This change specifically addresses scenarios where a session is resumed and the original PTY file descriptor has become stale, preventing unnecessary errors from being thrown. I have no feedback to provide as there were no review comments to evaluate.

@mini2s

mini2s commented May 22, 2026

Copy link
Copy Markdown
Contributor Author

/gemini review

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

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.

Code Review

This pull request modifies the ShellExecutionService in packages/core to include EBADF (bad file descriptor) in the list of ignored errors during PTY resizing. This change ensures that stale file descriptors encountered when resuming a session do not trigger unnecessary exceptions, aligning the behavior with existing handling for ESRCH and Windows-specific PTY errors. I have no feedback to provide.

@mini2s

mini2s commented May 22, 2026

Copy link
Copy Markdown
Contributor Author

@gemini-code-assist Please process this PR

@gemini-code-assist

Copy link
Copy Markdown
Contributor

I have reviewed the changes in packages/core/src/services/shellExecutionService.ts. The implementation correctly handles the EBADF error by adding it to the existing catch block, which is consistent with the current error handling strategy for stale PTY file descriptors. No further issues were identified.

@gemini-cli gemini-cli Bot added the priority/p1 Important and should be addressed in the near term. label May 22, 2026
@gemini-cli

gemini-cli Bot commented May 30, 2026

Copy link
Copy Markdown
Contributor

Hi there! Thank you for your interest in contributing to Gemini CLI.

To ensure we maintain high code quality and focus on our prioritized roadmap, we only guarantee review and consideration of pull requests for issues that are explicitly labeled as 'help wanted'.

This PR will be closed in 7 days if it remains without that designation. We encourage you to find and contribute to existing 'help wanted' issues in our backlog! Thank you for your understanding.

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

Labels

priority/p1 Important and should be addressed in the near term. size/xs An extra small PR status/pr-nudge-sent

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant