fix: defer PTY slave drop to prevent macOS data loss race#164
Merged
branchseer merged 2 commits intomainfrom Feb 16, 2026
Merged
fix: defer PTY slave drop to prevent macOS data loss race#164branchseer merged 2 commits intomainfrom
branchseer merged 2 commits intomainfrom
Conversation
On macOS, when the parent's slave fd is closed immediately after spawn and the child exits quickly, all slave references close before the reader issues its first read(). macOS returns EIO on the master PTY without draining the output buffer, causing data loss. Move the slave fd into the background monitoring thread and drop it after child.wait() returns. This keeps the PTY in a connected state while the child runs, guaranteeing buffered output is preserved regardless of reader timing.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes a flaky
is_terminaltest failure on macOS (pty_terminal::tests::terminal::is_terminal) observed in CI run #22038626101.Root Cause
Terminal::spawnpreviously dropped the parent's slave PTY fd immediately after spawning the child:On macOS, when a child process writes output and exits quickly, all slave references close before the reader ever calls
read()on the master. macOS then returnsEIOon the master PTY without draining the output buffer, causing the child's output to be permanently lost.portable-ptyconvertsEIOtoOk(0)(EOF), soread_to_endreturns with zero bytes andscreen_contents()is empty.The failure is a race between child exit and the first
read():read()pending before child exitsread()called after all slave fds closeReproduction
Adding
thread::sleep(Duration::from_secs(2))beforeread_to_endin theis_terminaltest deterministically reproduces the failure — the child always exits during the sleep, and the firstread()returns 0 with empty screen contents.Fix
Move the slave fd into the background monitoring thread instead of dropping it immediately in
spawn(). The thread holds the slave alive until afterchild.wait()returns, then drops writer and slave in sequence:This keeps the PTY in a "connected" state while the child runs, so the kernel output buffer is preserved regardless of when the reader starts.
Verification
read_to_end, the old code fails 100% of the time; the fix passes 100%.pty_terminaltest suite pass.cargo xtest): 9/9 tests pass onaarch64-pc-windows-msvc.cargo test(all crates) passes with no regressions.