Skip to content

fix(tui): exit when the terminal hangs up instead of spinning - #139

Merged
backnotprop merged 1 commit into
mainfrom
fix/hangup-spin
Nov 10, 2026
Merged

backnotprop merged 1 commit into
mainfrom
fix/hangup-spin

Conversation

@backnotprop

Copy link
Copy Markdown
Contributor

When the terminal closes and SIGHUP does not end the process (it is ignored, as under nohup, or the launcher does not forward it), plannotator-tui stays behind as an orphan at about 100% CPU. crossterm's unix reader only leaves its read loop on WouldBlock. After a hang-up, read returns Ok(0) forever, so event::poll never returns, whatever the timeout (crossterm-rs/crossterm#793, still open). The event loop never runs again.

New src/hangup.rs (unix only): right after ratatui::init, a thread polls the terminal every 500 ms and exits with status 129, the status SIGHUP would have left, when poll reports POLLHUP.

  • It watches stdin when stdin is a terminal, otherwise stdout. A pipe on stdin (last --stdin) is skipped, because a pipe whose writer has finished also reports POLLHUP.
  • It checks with poll, not tcgetattr/isatty. On macOS both keep succeeding on a pty whose controller has closed. I measured this before relying on it.
  • It calls ratatui::try_restore, not restore. restore prints its failure to stderr, which is the dead terminal too, and on my first try that print panicked inside ratatui's panic hook and aborted with status 134.
  • rustix is already in the lock file through crossterm. It is a new direct dependency only on unix (event, stdio, termios; pty for tests).

Checked on macOS in a private tmux server (trap '' HUP, then tmux kill-server):

before (main) after
hang-up, SIGHUP ignored orphan, PPID 1, state R, 99% CPU after 3 s; only SIGTERM ends it exits 0.1–0.5 s after the hang-up, status 129 (6 runs)
last --stdin with piped input, then hang-up already at 99% CPU before the hang-up (see below) exits, status 129

A normal session (q gives 0), SIGTERM (143), 3 s idle with piped stdin, and an $EDITOR handoff (o) all behave as before. The watch thread does not fire while the terminal is alive.

Tests: hangup::tests uses a real pty. It reads as hung up only after its controller is dropped, and a finished pipe on stdin is skipped in favour of the terminal. cargo fmt --all --check, cargo clippy --workspace --all-targets -- -D warnings, cargo test --workspace (297 in the app crate) pass. Windows: the module, its call and the dependency are cfg(unix). cargo clippy --target x86_64-pc-windows-msvc --workspace --all-targets -- -D warnings passes locally with a stub C compiler for the bundled sqlite. CI's check-windows is the real build.

Not fixed here, found while testing: on macOS, last --stdin with piped input already hangs on main at 99% CPU inside supports_keyboard_enhancement, before the event loop. crossterm cannot register /dev/tty with kqueue, so its query loops. That is a separate bug.

Refs #128

crossterm's unix reader never returns once its tty hangs up (read gives
Ok(0) forever, crossterm-rs/crossterm#793), so with SIGHUP ignored the
app stayed behind at 100% CPU. A watch thread polls the terminal on
stdin or stdout every 500 ms and exits with status 129 on POLLHUP.

Refs #128
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