A terminal program that changes the terminal's state beyond termios (enters the alternate screen, hides the cursor, turns on mouse reporting or bracketed paste) leaves the user's terminal that way when it traps (index out of range with !, integer overflow, a failed as).
A trap flushes stdout, prints trap: ... and calls exit(101): only C atexit handlers run. With the setRawMode work from #31 the runtime restores termios there, but the program has no way to have its own leaving sequence written, since no program code runs on a trap. The user is left on the alternate screen, with the trap message printed onto it.
Node runs 'exit' listeners after an uncaught exception, but running JavaScript from tv_trap is unsafe: the trap may fire inside a JSC callback, with runtime locks held, or on a worker thread.
Proposal (implemented on https://github.com/rofrol/Tov/tree/fix/41-tty-exit-reset, on top of the #31 branch): tty.WriteStream.prototype.setResetOnExit(sequence) registers bytes for the stream's terminal, on a copy of its fd; '' clears it. The runtime writes them once:
- at exit, after the last of stdout (
process.exit, the natural end, an uncaught error); - on a trap, after flushing stdout and before the
trap:message, so the message lands on the main screen (a hooktv_trap_cleanupintov.c, no JavaScript); - on a fatal SIGINT/SIGTERM while raw mode is on, from the signal handler (
writeonly).
A test (tests/npm/tty_reset.py) runs a program on a PTY for each way it can end and checks that the sequence comes exactly once, after the program's output, before the trap message on a trap, and never once cleared.
It is not a Node API; a name or place you prefer (process.setTerminalReset, say) is easy to change.
Activity
0No activity yet.