From 34e2d82a322bd3767a8ec62b3a4c0894399e8c04 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Sun, 22 Nov 2026 19:59:00 -0300 Subject: tty: a TERM that reaches a session while it leaves its terminal (a quit, a Detach, the detach a hangup makes) no longer leaves that terminal raw for good: `Screen.leave` let `Killed` go first and the cooked modes came back last (vaxis's `Tty.deinit`), after the kitty images, the alternate screen's exit (flushed at once) and vaxis's teardown, so a TERM in between found nothing to put back and died with the main screen back and the modes raw, nothing of pardes putting them back after it (at a quit the stub then dies of the same TERM, and a shell that does not reset its own modes prompts on a raw terminal); now `Killed.disarm` puts the cooked modes back itself and is called last, just before vaxis's own put-back, so a TERM anywhere in a leave finds them back or puts them back (dev/tui/tui.zig); the process case `killedTty` failed so in about one gate in ten (merge49 and merge58, "the terminal left raw" with the main screen back: its HUP round, which TERMs the session right after the stub dies of the hangup, while the session leaves the terminal that hung up) and now names its signal; not reproduced as it is (the base pinned to one CPU 0 of 40 failed, unpinned beside a second copy 0 of 80); with the window widened by an experiment's 300 ms sleep before vaxis's put-back and the test's TERM 100 ms after the stub's death, the base failed 10 of 10 with the gates' words ("HUP: the terminal left raw", the main screen back, 3.7-4.5 s as the gates' 3.6 and 4.2 s) and this passed 10 of 10 with the same sleep after its disarm; after: the case pinned 40 of 40, core-test 917/1074 (157 skipped), shell-test 1116/1119 (3 skipped), process-test 0 of 146 (load 10.6), as pairs 0 of 146 (load 11.7, `tty boot: ninep` refused a dial in the parallel batch and passed again alone); left as it is: on a hangup the stub dies at once (runtime/daemon.zig `forward`), before the session has left the terminal, so with the terminal still there (a `kill -HUP` of the job) the shell's prompt comes before the session's put-back, which then sets the cooked modes and the main screen over it (not raw) Co-Authored-By: Claude Opus Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- dev/tui/tui.zig | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) (limited to 'dev') diff --git a/dev/tui/tui.zig b/dev/tui/tui.zig index 9117d54d..5d75722f 100644 --- a/dev/tui/tui.zig +++ b/dev/tui/tui.zig @@ -901,12 +901,14 @@ const Screen = struct { fn leave(sc: *Screen) void { if (!sc.on) return; sc.on = false; - Killed.disarm(); clearNativeImages(&sc.kitty, &sc.vx, &sc.tty); sc.vx.exitAltScreen(sc.tty.writer()) catch {}; sc.vx.deinit(sc.gpa, sc.tty.writer()); if (comptime !builtin.is_test) vaxis.tty.global_tty = null; - sc.tty.deinit(); // restore cooked termios LAST, after vx flushed its resets + // The cooked modes back LAST, after vx flushed its resets, and Killed + // armed until they are. + Killed.disarm(); + sc.tty.deinit(); } /// Its terminal hung up, or the stub that waits on it is gone. @@ -950,10 +952,14 @@ const Killed = struct { _ = std.c.tcsetattr(tty, .FLUSH, termios); } - /// The terminal gone (put back, or detached): a TERM still takes the - /// socket, the prompt files and the shells with it, and leaves the - /// terminal alone. + /// The terminal let go (Screen.leave): its cooked modes put back first, + /// so that a TERM anywhere in a leave finds them back or puts them back + /// itself (let go first, a TERM before vaxis put them back died leaving + /// the terminal raw for good); then a TERM still takes the socket, + /// the prompt files and the shells with it, and leaves the terminal + /// alone. fn disarm() void { + if (fd >= 0) _ = std.c.tcsetattr(fd, .FLUSH, &cooked); fd = -1; } -- cgit v1.3