fix(pty): preserve concurrent fast-exit output#8
Open
privatenumber wants to merge 7 commits into
Open
Conversation
privatenumber
marked this pull request as ready for review
July 14, 2026 00:21
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.
Problem
On Unix, a fast child could exit before JavaScript drained the PTY master, allowing
pty.exitedto resolve before pending output callbacks. A Node 24.15.0 macOS arm64 reproduction lostFASTin 52/100 concurrentonDataruns and 55/100 concurrentterminal.dataruns; sequential runs lost none.Moving reads native also requires explicit ownership across pause, close, Worker teardown, raw callbacks, and process trees. Without coordinated ownership, queued callbacks can outlive close, recycled fd/PID values can target unrelated resources, and descendants can retain the PTY after their leader exits.
Changes
Unix now starts the child suspended, starts a native reader, and then resumes the child. Data and exit share one bounded Node-API queue, so exit cannot overtake output delivery. Pause applies bounded native backpressure; close suppresses queued callbacks and forces deterministic cleanup even when SIGHUP is ignored.
JavaScript owns the returned fd while native reading uses a CLOEXEC duplicate. Worker cleanup closes the public fd only when its identity still matches the original PTY, preventing recycled-fd double-close. The exit monitor observes leader exit with
waitid(..., WNOWAIT), keeping a non-recyclable process-group anchor until same-group descendants and the reader are cleaned; intentionally detached descendants remain caller-owned.Regressions cover 100 concurrent fast exits, Darwin pause/resume, callback reentrancy, ignored-SIGHUP and Worker disposal, fd close/reuse, leader-first process groups, real fault injection, and repeated thread/fd cleanup. Fixed direct reproductions lose 0/100 outputs through both callback APIs.