gemini-cli - ✅(Solved) Fix run_shell_command: stdout lost on fast commands due to child.on('exit') vs child.on('close') race in shellExecutionService [2 pull requests, 1 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
google-gemini/gemini-cli#24923Fetched 2026-04-09 08:17:23
View on GitHub
Comments
0
Participants
1
Timeline
2
Reactions
0
Author
Participants
Timeline (top)
labeled ×2

Root Cause

packages/core/src/services/shellExecutionService.ts line ~731 in childProcessFallback() uses:

child.on('exit', (code, signal) => {
  handleExit(code, signal);
});

Node.js documentation explicitly warns:

It is important to note that when the 'exit' event is triggered, child process stdio streams might still be open.

For very fast commands, the exit event fires before all stdout.on('data') callbacks have delivered pipe buffer contents to state.output. The close event waits for stdio streams to finish and is the correct event for this pattern.

PR fix notes

PR #25311: fix(core): use close event instead of exit for child_process

Description (problem / solution / changelog)

Fixes #24923 by changing child.on(exit) to child.on(close) to prevent truncating short commands.

Changed files

  • packages/core/src/services/shellExecutionService.ts (modified, +1/-1)

PR #25329: fix(core): ensure output streams flush before teardown

Description (problem / solution / changelog)

Fixes #24923. Fast commands lost stdout because we exited on \exit\ instead of \close.

Changed files

  • packages/core/src/services/shellExecutionService.ts (modified, +1/-1)

Code Example

child.on('exit', (code, signal) => {
  handleExit(code, signal);
});

---

child.on('close', (code, signal) => {
  handleExit(code, signal);
});
RAW_BUFFERClick to expand / collapse

Bug Description

Fast shell commands like bare ls return empty stdout (exit code 0) even in non-empty directories. Slightly slower variants like ls -F (which stat()s each entry) return correct output.

Steps to Reproduce

  1. Run Gemini CLI in ACP mode (--experimental-acp --yolo)
  2. Ask: "exec ls command"
  3. Observe: run_shell_command completes with exit code 0 but empty output
  4. Ask: "exec ls -F command"
  5. Observe: correct file listing is returned

Reproduces consistently in Docker containers (bun:1.2 base image) where node-pty is unavailable, forcing the child_process fallback path.

Root Cause

packages/core/src/services/shellExecutionService.ts line ~731 in childProcessFallback() uses:

child.on('exit', (code, signal) => {
  handleExit(code, signal);
});

Node.js documentation explicitly warns:

It is important to note that when the 'exit' event is triggered, child process stdio streams might still be open.

For very fast commands, the exit event fires before all stdout.on('data') callbacks have delivered pipe buffer contents to state.output. The close event waits for stdio streams to finish and is the correct event for this pattern.

Evidence from Timing

CommandDurationstdout
ls~24msempty
ls -F~30mscorrect listing

The 6ms difference (due to -F calling stat() per entry) is enough for the pipe buffer to flush before the exit handler runs.

Proposed Fix

Change child.on('exit', ...) to child.on('close', ...) at that line:

child.on('close', (code, signal) => {
  handleExit(code, signal);
});

The close event fires after all stdio streams are fully consumed, guaranteeing state.output contains the complete stdout/stderr data.

Environment

  • Gemini CLI: installed via bun install -g @google/gemini-cli
  • Runtime: Bun 1.2 in Docker (oven/bun:1.2)
  • OS: Linux (Debian-based container)
  • PTY: unavailable (no native build tools in container), falls back to child_process.spawn()
  • Mode: ACP (--experimental-acp --yolo)

Notes

  • PTY mode is likely unaffected since it writes to an xterm headless terminal buffer synchronously
  • The cleanup() function inside handleExit does call stdoutDecoder.decode() for remaining bytes, but the issue is that stdout.on('data') events haven't fired yet, not that bytes are stuck in the decoder
  • Related: #22170 and #23751 touched shell output buffering but not this specific race

extent analysis

TL;DR

Change the event listener from child.on('exit', ...) to child.on('close', ...) in childProcessFallback() to ensure that the handleExit function is called after all stdio streams are fully consumed.

Guidance

  • Identify the line of code causing the issue: packages/core/src/services/shellExecutionService.ts line ~731.
  • Replace child.on('exit', (code, signal) => { handleExit(code, signal); }); with child.on('close', (code, signal) => { handleExit(code, signal); }); to fix the race condition.
  • Verify the fix by running the Gemini CLI in ACP mode and checking the output of fast shell commands like ls.
  • Test the fix with different commands and scenarios to ensure it works consistently.

Example

// Before
child.on('exit', (code, signal) => {
  handleExit(code, signal);
});

// After
child.on('close', (code, signal) => {
  handleExit(code, signal);
});

Notes

The proposed fix assumes that the close event is the correct event to use in this scenario, as it waits for stdio streams to finish. However, it's essential to test the fix thoroughly to ensure it works as expected in all cases.

Recommendation

Apply the workaround by changing the event listener to child.on('close', ...) to fix the issue with fast shell commands returning empty stdout. This change ensures that the handleExit function is called after all stdio streams are fully consumed, providing the correct output.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING