diff options
| -rw-r--r-- | dev/tui/tui.zig | 23 | ||||
| -rw-r--r-- | runtime/client.zig | 5 | ||||
| -rw-r--r-- | test/daemon.py | 42 |
3 files changed, 62 insertions, 8 deletions
diff --git a/dev/tui/tui.zig b/dev/tui/tui.zig index 72028b04..67c0e2a0 100644 --- a/dev/tui/tui.zig +++ b/dev/tui/tui.zig @@ -721,19 +721,22 @@ pub fn runClient(init: std.process.Init, opts: pardes.Options, path: []const u8, std.process.exit(1); } }; - var start: client_loop.Start = undefined; - if (!start.open(init, opts, .{ .path = path, .session = session, .far = far_link })) Said.no(io, "{s}", .{start.said()}); - defer start.close(); - + // Blocked before its join makes a thread, so that winchWatch alone + // takes them: a hangup (its terminal closed) or a TERM ends it the + // normal way (`quit`), its goodbye a close (a pair's session not kept + // ends at its last client's), what it made kept on disk (`Have.keep`) + // and its ssh forward's socket unlinked (main.zig). var winch_set = posix.sigemptyset(); posix.sigaddset(&winch_set, posix.SIG.WINCH); - // A hangup ends it the normal way (`quit`), what it made kept on disk - // (`Have.keep`) and its ssh forward's socket unlinked (main.zig). posix.sigaddset(&winch_set, posix.SIG.HUP); + posix.sigaddset(&winch_set, posix.SIG.TERM); hangup_quits = true; if (comptime builtin.os.tag == .freebsd) posix.sigaction(.WINCH, &.{ .handler = .{ .handler = keepSignal }, .mask = posix.sigemptyset(), .flags = 0 }, null); posix.sigprocmask(posix.SIG.BLOCK, &winch_set, null); + var start: client_loop.Start = undefined; + if (!start.open(init, opts, .{ .path = path, .session = session, .far = far_link })) Said.no(io, "{s}", .{start.said()}); + defer start.close(); var tty_buf: [0x10000]u8 = undefined; var screen: Screen = .{ .io = io, .gpa = gpa, .env = init.environ_map, .buf = &tty_buf, .kitty = .init(gpa) }; defer screen.kitty.deinit(); @@ -1544,7 +1547,10 @@ const Shell = struct { virtual.* = @max(virtual.*, s.wake_to); }, .quit => { + // A client's terminal went (a hangup, its input's end) or it + // took a TERM: a close, as a window's close button. if (s.client) |c| { + c.link.close = true; c.link.quit = true; return true; } @@ -1939,19 +1945,20 @@ const Shell = struct { fn keepSignal(_: posix.SIG) callconv(.c) void {} -/// A replica client's (`runClient`): a hangup is its end, whoever sent it. +/// A replica client's (`runClient`): a hangup or a TERM is its end. var hangup_quits = false; fn winchWatch(loop: *Loop) void { var set = posix.sigemptyset(); posix.sigaddset(&set, posix.SIG.WINCH); if (daemon.link >= 0 or hangup_quits) posix.sigaddset(&set, posix.SIG.HUP); + if (hangup_quits) posix.sigaddset(&set, posix.SIG.TERM); while (true) { var sig: c_int = 0; if (libc.sigwait(&set, &sig) != 0) continue; // The terminals are the loop's: it asks each its size, and takes a // hangup as the end of the one that hung up. - const ev: Msg = if (sig != @backingInt(posix.SIG.HUP)) .winched else if (hangup_quits) .quit else .hangup; + const ev: Msg = if (sig == @backingInt(posix.SIG.WINCH)) .winched else if (hangup_quits) .quit else .hangup; loop.postEvent(ev) catch {}; } } diff --git a/runtime/client.zig b/runtime/client.zig index 329bdab3..7941889e 100644 --- a/runtime/client.zig +++ b/runtime/client.zig @@ -149,6 +149,11 @@ pub fn execAgain(s: *Start, view: u8) []const u8 { k += 1; } envp[k] = null; + // The mask goes across an exec: the signals a terminal client blocks + // for its own sigwait (HUP, TERM) would stay blocked under a build that + // waits on fewer, a TERM never delivered. + const none = std.posix.sigemptyset(); + std.posix.sigprocmask(std.posix.SIG.SETMASK, &none, null); _ = libc.execve(path, &argv, envp.ptr); return "Upgrade: the build at this client's start path would not start"; } diff --git a/test/daemon.py b/test/daemon.py index ce2ffbf1..aef37d0a 100644 --- a/test/daemon.py +++ b/test/daemon.py @@ -770,6 +770,48 @@ def pair_rows(binary): assert subprocess.run([binary, 'kill', 'kd'], env=env, capture_output=True, timeout=30).returncode == 0 wait_for('kd ended', lambda: not alive(kd), timeout=30) print('pair rows: `new --detach` kept from birth (ps: (tty, kept)); its client ended by HUP, by TERM: it runs on') + # Not kept: a client's close (its terminal closed, a TERM) ends the + # session when it was the last; a KILL is a loss, it runs on. + t, c, nine, session, pane = started('nk', env) + assert ended_by('HUP', *attached('nk')) == 0 + assert alive(session) and state_of('nk') == '(tty)', ('not its last client', listed()) + os.kill(c.pid, signal.SIGKILL) + c.wait(timeout=10) + wait_for('the client unlisted', lambda: f'client {c.pid} ' not in listed()) + assert alive(session) and state_of('nk') == '(tty)', ('a loss', listed()) + assert ended_by('TERM', *attached('nk')) == 0 + wait_for('the session ended at its last client\'s close', lambda: not alive(session), timeout=20) + assert state_of('nk') is None, listed() + nine.socket.close() + print('pair rows: not kept: a client\'s terminal closed (not the last): it runs on; its own killed -9: it runs on, ' + 'listed; the last client TERMed (exit 0, its terminal put back): the session ended') + # The pair's own terminal closed after an Upgrade: its last client's + # close, which the Upgrade did not make kept. + t, c, nine, session, pane = started('nk2', env) + nine = upgraded('nk2', nine, session) + wait_for('the client joined again', lambda: b'joined again' in t.text(), timeout=30) + nine.socket.close() + t.close() + terms.remove(t) + assert c.wait(timeout=20) == 0, c.returncode + wait_for('the session ended with its terminal', lambda: not alive(session), timeout=20) + print('pair rows: not kept, upgraded: its terminal closed (exit 0): the session ended') + # Without the variable nothing changes: an attach client's TERM + # leaves the session running, and its own terminal closed detaches it. + own_env = {k: v for k, v in env.items() if k != 'PARDES_PAIR'} + t, c, nine, session, pane = started('own', own_env) + assert state_of('own') == '(tty, has a frontend)', listed() + assert ended_by('TERM', *attached('own')) == 0 + assert alive(session) and state_of('own') == '(tty, has a frontend)', listed() + nine.socket.close() + t.close() + terms.remove(t) + c.wait(timeout=20) + wait_for('the own session detached', lambda: state_of('own') == '(tty, detached)') + assert alive(session) + assert subprocess.run([binary, 'kill', 'own'], env=env, capture_output=True, timeout=30).returncode == 0 + wait_for('the own session ended', lambda: not alive(session), timeout=30) + print('pair rows: without the variable: an attach client TERMed, the session on; its terminal closed, it detached (ps: (tty, detached))') # A pair: not kept, and not across an Upgrade either; a Detach typed # in its client keeps it, across an Upgrade too. t, c, nine, session, pane = started('rows', env) |
