Skip to content

Restore the terminal when a fault or abort signal ends PrismTTY - #251

Merged
inxbit merged 5 commits into
mainfrom
fix/fatal-signal-terminal-restore
Dec 4, 2026
Merged

inxbit merged 5 commits into
mainfrom
fix/fatal-signal-terminal-restore

Conversation

@inxbit

@inxbit inxbit commented Dec 4, 2026 •

Copy link
Copy Markdown
Owner

What was wrong

Two things, measured on 1.6.8 (macOS, Apple silicon):

  1. SIGABRT, SIGTRAP, SIGILL, SIGFPE, SIGSYS and SIGEMT ended PrismTTY with the terminal still in raw mode (no echo, no line editing, Ctrl-C dead) until the user typed reset.
  2. An outside kill -SEGV or kill -BUS made PrismTTY print I/O error: Interrupted system call (os error 4) and exit 1. Rust's own stack-overflow handler takes these two signals, resets them and returns, and that return interrupted the stream read.

What changes

  • A last-chance handler for the fatal signals restores the saved terminal mode (only while PrismTTY owns the terminal foreground), puts the default action back and sends the signal again to the thread that handled it, so PrismTTY still dies from that signal. For SIGBUS and SIGSEGV it first calls the handler that was there before, so a real Rust stack overflow still prints its report and ends in SIGABRT.
  • The two top-level stream reads retry a read that a returning signal handler interrupted.
  • The fatal actions are installed before the signal watcher thread starts, and one session per process owns this path.
  • Pipe mode (command | prismtty) starts no terminal session and is unchanged.

Measured (19 rows, 1.6.8 against this branch)

Signal sent to PrismTTY on a terminal 1.6.8 this branch
SIGBUS, SIGSEGV exit 1 with the I/O error, mode restored dies from the signal (138, 139), mode restored
SIGABRT, SIGTRAP, SIGILL, SIGFPE, SIGSYS, SIGEMT dies from the signal, terminal left raw dies from the signal, complete mode restored
SIGTERM exit 143, mode restored unchanged
the nine pipe-mode rows unchanged
SIGABRT while the command ignores SIGHUP terminal left raw mode restored; the command keeps running, as documented

Real faults (a null store, an undefined instruction) and a real Rust stack overflow are covered by tests that check the wait status and the complete terminal mode. On macOS, the crash reports of one measured run still showed the original fault: for the null store the exception type, the address, the thread and the faulting frame; for the undefined instruction the exception type, the thread and the faulting frame.

Limits (in the README)

Best effort: no restore when PrismTTY does not own the terminal foreground at the check, or when the terminal write fails; a signal that was ignored or already handled when the session began is left alone; this path does not draw buffered output or tear down the command; if the default action cannot be installed or nothing can be sent, PrismTTY exits with 128 plus the signal number instead.

For programs that use the library rather than the command: when sessions overlap in one process only the first has this last-chance restore, and a thread that is already inside an earlier SIGBUS or SIGSEGV handler when the session starts can still put the default action back.

Not measured

Core files. On Linux the new tests ran for the first time in this pull request's CI (Ubuntu, x86-64) and pass, including the real-fault, abort and stack-overflow tests; the 19-row table itself was measured on macOS only.

Tests

macOS, Apple silicon: fmt and clippy clean, 1339 Rust tests and the node tests pass, privacy scan clean. Each new test was run 10 or 20 times in a row, and the whole library suite three times, with no failure. The new start-order test fails on the old order and passes on the new.

SIGABRT, SIGTRAP, SIGILL, SIGFPE, SIGSYS, SIGBUS, SIGSEGV and, on macOS,
SIGEMT ended PrismTTY with its terminal still in raw mode. A last-chance
handler now restores the saved terminal mode, chains the runtime's
stack-overflow handler for SIGBUS and SIGSEGV, puts the default action
back and sends the signal again, so PrismTTY still dies from it.

A SIGBUS or SIGSEGV sent from outside interrupted the stream read and
became an I/O error with exit 1. The read is retried now, and in a
wrapped session the signal ends PrismTTY as itself.
…ved predecessor published, withdraw only a new snapshot
@inxbit
inxbit marked this pull request as ready for review December 4, 2026 22:42
@inxbit
inxbit merged commit 4fa50ff into main Dec 4, 2026
6 checks passed
@inxbit
inxbit deleted the fix/fatal-signal-terminal-restore branch December 4, 2026 22:43
@inxbit inxbit mentioned this pull request Dec 4, 2026
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