Skip to content

fix(last): read keys from the terminal after --stdin consumed stdin - #167

Merged
backnotprop merged 1 commit into
mainfrom
fix/stdin-spin
Nov 20, 2026
Merged

backnotprop merged 1 commit into
mainfrom
fix/stdin-spin

Conversation

@backnotprop

Copy link
Copy Markdown
Contributor

Cause

On macOS, plannotator-tui last --stdin < file showed an empty alternate screen and used 100% of a CPU forever. Reproduced on main in a private tmux with a scratch HOME; sample puts the spin in crossterm::terminal::supports_keyboard_enhancement, called from cli::with_terminal before the first draw:

  • stdin is a file, so crossterm's unix event source (mio) opens /dev/tty and registers it with kqueue. macOS refuses that, so the event reader has no source.
  • query_keyboard_enhancement_flags_raw loops on poll_internal and ignores errors (Err(_) => {}), so the immediate "Failed to initialize input reader" error turns into a busy loop. Even without that query, no key could ever be read.

Linux's epoll accepts /dev/tty, so this is macOS-only as far as I can tell (Linux not verified here).

Fix

After last --stdin has read the document, open the terminal's own device and dup2 it onto stdin. The device is named by ttyname of stdout or stderr (for example /dev/ttys003); /dev/tty is the fallback when neither is a terminal. kqueue accepts the real device. crossterm, the hang-up watcher and the keyboard query then all take the ordinary "stdin is a terminal" path. This adds the fs feature to the existing rustix dependency, which ttyname needs. No new crates.

The other fix, crossterm's use-dev-tty feature, would switch input handling for the whole build, which the diffs-rs comment in Cargo.toml already avoids.

Verified

  • In a private tmux at 120x40 with scratch HOME and PLANNOTATOR_DATA_DIR: main stays blank at ~99% CPU. With this branch the document draws, CPU is 0%, G goes to block 62/62, and q exits 0.
  • cargo fmt --check, cargo clippy --locked --workspace --all-targets -- -D warnings and cargo test --locked --workspace pass on macOS.

No automated test: it would have to replace the test process's own stdin with a pty.

With stdin redirected, crossterm registers /dev/tty with mio, which kqueue on
macOS refuses; its keyboard-protocol query then retries the failed poll forever,
so the screen stayed blank at full CPU. Once the document is read, put the
terminal's own device (named from stdout or stderr) on stdin.
@backnotprop
backnotprop added this pull request to the merge queue Nov 20, 2026
Merged via the queue into main with commit 65c1a20 Nov 20, 2026
3 checks passed
@backnotprop
backnotprop deleted the fix/stdin-spin branch November 20, 2026 01:09
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