Repository navigation
fix(pty): let a write give up, and stop teardown hanging on a full queue - #295
Merged
Merged
Conversation
A terminal whose foreground program stops reading fills its input queue, and a blocking write into it never returns. Two separate faults follow. `PtySession::write` parks in the kernel, so a caller with anything else to watch — a disconnected client, a shutdown — never gets control back. That state cannot be interrupted: not by cancelling the thread, and not by signalling or killing the process holding the other end. Adds `PtySession::write_available`, which writes as much as the terminal accepts before a timeout and returns `0` when the queue stayed full, over a non-blocking window on the master. Teardown hangs on its own. Dropping the session's writer sends a newline and the terminal's EOF character through the master, and that write blocks while the queue is full — so teardown hangs for exactly the session that filled it, the one whose client gave up mid-write and most needs releasing. Adds `PtyMaster::prepare_for_teardown`, which the session drop calls before its fields drop. Found downstream: fastled-wasm caps concurrent terminals with a semaphore the PTY worker holds, so a parked write leaked a slot until the foreground program exited on its own — 27s in its fixture. Both faults had to be fixed; either alone still leaked the slot, because the worker would break out of the write only to hang in drop. Backends without a bounded write fall back to the blocking write and report that the interruption guarantee is Unix-only. Co-Authored-By: Claude Code <[email protected]>
Release on release/0.1.3 containing the bounded PTY write and the teardown fix, on top of v0.1.10. Co-Authored-By: Claude Code <[email protected]>
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
A terminal whose foreground program stops reading fills its input queue. A blocking write into it never returns, and that state cannot be interrupted — neither by cancelling the thread, nor by signalling or killing every process holding the other end. Measured: SIGKILL the whole tree during a parked 1 MiB write and the writer is still parked five seconds later.
Two separate faults follow, and either one alone is enough to break a caller.
1. The caller never regains control.
PtySession::writeparks in the kernel, so a service with something else to watch — a disconnected client, a shutdown — cannot stop.2. Teardown hangs. Even having given up on the write, dropping the session hangs. portable-pty's
UnixMasterWriter::dropsends a newline and the terminal's EOF character through the master:On a full queue that write never returns, so teardown hangs for exactly the session that filled it — the one whose client gave up mid-write and most needs releasing. Measured directly: dropping a session with an empty queue returns in 0.05 s; dropping one with a full queue hangs indefinitely.
Fix
PtySession::write_available(bytes, timeout)writes as much as the terminal accepts before the timeout and returns0when the queue stayed full. Implemented over a non-blocking window on the master (O_NONBLOCK+poll), restored afterwards because status flags belong to the open file description and are shared with every duplicate.PtyMaster::prepare_for_teardown(), called byPtySession::dropbefore its fields drop, marks the master non-blocking so the writer's final write gives up instead of waiting.Backends without a bounded write fall back to the blocking write, so the API is always usable; only the interruption guarantee is Unix-only, and that is stated rather than implied.
Downstream
zackees/fastled-wasmcaps concurrent terminals with a semaphore its PTY worker holds. A parked write leaked a slot until the foreground program exited by itself — 27 s in its fixture, whose blocking command issleep 30. Both faults had to be fixed: fixing only the write left the worker breaking out of the write and then hanging in drop, still holding the slot.Tests
bounded_write_returns_instead_of_parking_on_a_full_queue— asserts a write against a full queue returns0inside its timeout.teardown_does_not_hang_on_a_full_input_queue— drops on its own thread so a regression fails the test rather than hanging it.Both had to be made deterministic: filling a fixed number of bytes races the child's startup, because before
stty rawruns the line discipline is still cooked, where input is consumed rather than queued and the queue never fills. The tests now wait for a marker the child prints after switching to raw mode. That race is why an earlier version of these tests passed in isolation and failed in the full run.Validation
soldr cargo test --all-features -j 4— 493 passed; the 3 failures inplatform_linux/snapshotare ELF build-id and stack-symbol tests, pre-existing on this host and failing identically on the unmodified branch.soldr cargo fmt --all -- --checkandsoldr cargo clippy --all-features --all-targetsclean.0.1.11so the downstream consumer can pin an exact release.macOS mirrors the Linux implementation; this host cannot compile it, so
run-on-macosis the check.🤖 Generated with Claude Code