Skip to content

avoid hang after child exit when descendants keep stdio open - #3649

Closed
machado2 wants to merge 5 commits into
openai:mainfrom
machado2:bugfix/hang-after-shell
Closed

machado2 wants to merge 5 commits into
openai:mainfrom
machado2:bugfix/hang-after-shell

Conversation

@machado2

@machado2 machado2 commented Sep 15, 2025 •

Copy link
Copy Markdown

Codex was hanging after executing playwright on a project, and I had it fix itself, explaining the problem with:

"Some commands (e.g. test runners like Playwright) spawn grandchildren that inherit stdout/stderr. Those descendants may keep the inherited FDs open after the main child process exits. Our previous implementation waited for EOF on the pipes, so we could wait forever even though we already observed the child exit (or timeout), leaving Codex blocked."

It's changes seem correct, and it also corrected one of the tests that had a bug running on a non-US machine, adding a formatter with a fixed US locale.

I verified that the problem happens on the main branch and the change fixed it.

@github-actions

github-actions Bot commented Sep 15, 2025 •

Copy link
Copy Markdown
Contributor

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@machado2

Copy link
Copy Markdown
Author

I have read the CLA Document and I hereby sign the CLA

@machado2

Copy link
Copy Markdown
Author

Link for the related issue:

#3204

@yevon

yevon commented Oct 17, 2025

Copy link
Copy Markdown

Any news on this one? Codex should be able to terminate commands itself after a while,.like github copilot does by verifying the process response every x seconds.

@etraut-openai

Copy link
Copy Markdown
Contributor

@codex review

@etraut-openai

Copy link
Copy Markdown
Contributor

Thanks for the contribution, and apologies for the slow response. We received a large number of PRs and are just now catching up on our backlog.

If you're still interested in pursuing this PR, could you please resolve the merge conflicts?

@etraut-openai etraut-openai added the needs-response Additional information is requested label Nov 4, 2025

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread codex-rs/core/src/exec.rs Outdated
Comment on lines +364 to +368
maybe_chunk = agg_rx.recv() => {
if let Ok(chunk) = maybe_chunk {
append_all(&mut combined_buf, &chunk);
} else {
// aggregate channel closed; keep draining others until deadline

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.

P1 Badge Starvation leaves stdout/stderr always empty

After switching to tokio::select! to drain the three channels, the first branch listens to agg_rx.recv() and the macro is biased toward the first ready future. Once the aggregate channel closes (which happens as soon as the readers finish), recv() immediately returns Err each iteration and the loop never polls stdout_rx or stderr_rx before the 300 ms deadline. As a result, stdout_buf and stderr_buf remain empty for normal commands, so ExecToolCallOutput.stdout and .stderr will always be blank even when the process produced output. Consider removing closed channels from the select or iterating until each per-stream channel is drained.

Useful? React with 👍 / 👎.

Some commands (e.g. test runners like Playwright) spawn grandchildren that inherit stdout/stderr. Those descendants may keep the inherited FDs open after the main child process exits. Our previous implementation waited for EOF on the pipes, so we could wait forever even though we already observed the child exit (or timeout), leaving Codex blocked.

This change ensures we don’t block indefinitely:
- Stream stdout/stderr into channels while the child is alive.
- After the child exits, drain available output for a short grace period (300ms).
- Abort the reader tasks if they’re still blocked, so we don’t rely on EOF from descendants.

Behavioral notes:
- We may miss late bytes that arrive after the grace window in pathological cases. This is an intentional trade-off to guarantee termination.
- Aggregated output still collects during runtime; the grace period just sweeps remaining buffered data.

Files:
- codex-rs/core/src/exec.rs: adjust consume_truncated_output and read_capped to use per-stream channels and timed drain, then abort readers.

Rationale:
- Fixes a class of hangs observed after commands report a non-zero exit (e.g. npm run e2e) but stdout/stderr aren’t closed due to lingering descendants.
…in-exectoolcalloutput

Fix exec drain loop after aggregate channel closes
@etraut-openai etraut-openai removed the needs-response Additional information is requested label Nov 9, 2025
youta7 added a commit to youta7/ta-codex that referenced this pull request Nov 10, 2025
@etraut-openai

Copy link
Copy Markdown
Contributor

After reviewing the fix proposed in this PR, we think that there is a simpler way to address the bug. We're pursuing that fix here. I'm going to close this one.

Thanks again for taking the time to submit this PR.

swordfish444 pushed a commit to swordfish444/codex that referenced this pull request Feb 26, 2026
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.

3 participants