Skip to content

fix(pty): let a write give up, and stop teardown hanging on a full queue - #295

Merged
zackees merged 2 commits into
release/0.1.3from
feat/pty-bounded-write
Sep 16, 2026
Merged

zackees merged 2 commits into
release/0.1.3from
feat/pty-bounded-write

Conversation

@zackees

@zackees zackees commented Sep 16, 2026

Copy link
Copy Markdown
Collaborator

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::write parks 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::drop sends a newline and the terminal's EOF character through the master:

if eot != 0 {
    let _ = self.fd.0.write_all(&[b'\n', eot]);   // blocking
}

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 returns 0 when 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 by PtySession::drop before 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-wasm caps 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 is sleep 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 returns 0 inside 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 raw runs 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 in platform_linux/snapshot are ELF build-id and stack-symbol tests, pre-existing on this host and failing identically on the unmodified branch.
  • soldr cargo fmt --all -- --check and soldr cargo clippy --all-features --all-targets clean.
  • Bumps to 0.1.11 so the downstream consumer can pin an exact release.

macOS mirrors the Linux implementation; this host cannot compile it, so run-on-macos is the check.

🤖 Generated with Claude Code

zackees and others added 2 commits September 16, 2026 15:53
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]>
@zackees
zackees merged commit da40ae5 into release/0.1.3 Sep 16, 2026
53 checks passed
@zackees
zackees deleted the feat/pty-bounded-write branch November 5, 2026 05:51
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.

1 participant