From 1f731da28bd9983cc414839bd32372d183438164 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 20 Nov 2026 14:33:20 -0300 Subject: pair: `kept`, whole (P36): a session's state the daemon hears is one of attached, detached, pair, kept (daemon.State, its hello's and `state` word, numbered as the daemon's record said its bool), a `new --detach` (and the bare `--detach`) under PARDES_PAIR kept from birth, a pair's session `pair` until a Detach typed in any client makes it kept (daemon.keepSession, never cleared), an Upgrade carrying it (the record's `state` word), and Peers.kept deleted: a client's close ends the session only while it is `pair` (Peers.leave), the end condition no longer asks Options.pair but for the daemonless pair; `pardes ps` says `(tty)` of a pair's session not kept and `(tty, kept)` of a kept one, an own-path session's line unchanged (`(tty, has a frontend)`, `(tty, detached)`), and a bare attach takes any session without a frontend; test/daemon.py `pair_rows` rewritten (a `new --detach` kept, its clients ended by HUP and TERM: runs on; a pair `(tty)` across an Upgrade, its Detach `(tty, kept)` across another, its clients ended by TERM, KILL, HUP: runs on), `pair` and `gui_pair` read the new line, and gui_pair closes a window client of a `new --detach` session: it runs on; on the base `--pair` fails at "the client listed" (ps said `(tty, detached)`), here it passes (load 5-8), shell-test's record and bare-attach tests 32/32 and 9/9 Co-Authored-By: Claude Opus Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- test/daemon.py | 119 ++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 92 insertions(+), 27 deletions(-) (limited to 'test/daemon.py') diff --git a/test/daemon.py b/test/daemon.py index b28d16ea..ce2ffbf1 100644 --- a/test/daemon.py +++ b/test/daemon.py @@ -635,7 +635,7 @@ def pair(binary): assert session != c.pid and ppid(session) == c.pid and os.getsid(session) == session, (session, c.pid) wait_for('the file drawn on the terminal', lambda: b'line 3 of the notes' in t.text(), timeout=30) listed = lambda: subprocess.run([binary, 'ps'], env=env, capture_output=True).stdout.decode() - wait_for('the client listed', lambda: f'session pair {session} (tty, detached)' in listed() + wait_for('the client listed', lambda: f'session pair {session} (tty)' in listed() and f'client {c.pid} ' in listed()) pane = next(int(row.split()[0]) for row in nine.read('/index').splitlines() if row.split()[1] == b'text') os.write(t.master, b'Ofrom-the-pair\x1b') @@ -676,12 +676,13 @@ def pair(binary): def pair_rows(binary): """PARDES_PAIR, the shell's view (the last step's design, 1.1) and a - session's lifetime (2.2 B), one row a step. With a daemon: a Detach in - the pair's client says so and exits 0, the session runs on (listed); an - attach is a client again; TERM to the client puts its terminal back and - the session runs on; so does a KILL, and a closed terminal (HUP): each a - loss, not a close. With none: a Detach refused (nothing could attach), - the client on; its terminal closed, the session ends with its last link.""" + session's lifetime (2.2 B), one row a step. `ps` says a pair's session + `kept` (started --detach, or a Detach typed in any client since), or + nothing after its kind; an Upgrade carries which. Kept: a client ended + by TERM, KILL or a closed terminal (HUP) leaves it running, as does a + Detach typed in one (said, exit 0). With no daemon: a Detach refused + (nothing could attach), the client on; its terminal closed, the session + ends with its last link.""" scratch = Path(__file__).resolve().parent.parent.parent / '.scratch' / 'daemon' / 'dmn' scratch.mkdir(parents=True, exist_ok=True) root = Path(tempfile.mkdtemp(prefix='rq-', dir=scratch)) @@ -697,6 +698,12 @@ def pair_rows(binary): def listed(): return subprocess.run([binary, 'ps'], env=env, capture_output=True).stdout.decode() + def state_of(name): + """What `ps` says of session `name` after its pid: `(tty)`, `(tty, kept)`.""" + for line in listed().splitlines(): + if line.split()[:2] == ['session', name]: + return line[line.index('('):] + def drawn(t): wait_for('drawn', lambda: b'line 3 of the notes' in t.text(), timeout=30) @@ -717,30 +724,70 @@ def pair_rows(binary): drawn(t) return t, c, nine, session, pane + def attached(name): + tt = Term(30, 100) + terms.append(tt) + a = tt.spawn([binary, 'attach', name], env) + drawn(tt) + wait_for('the client listed', lambda: f'client {a.pid} ' in listed()) + return tt, a + + def ended_by(how, tt, a): + """Client `a` on terminal `tt` ended: TERM, KILL, or HUP (its terminal + closed); its exit status, once it is unlisted.""" + if how == 'HUP': + tt.close() + terms.remove(tt) + else: + os.kill(a.pid, getattr(signal, 'SIG' + how)) + code = a.wait(timeout=20) + if how == 'TERM': + wait_for('its terminal put back', lambda: tt.text().rfind(b'\x1b[?1049l') > tt.text().rfind(b'\x1b[?1049h')) + wait_for('the client unlisted', lambda: f'client {a.pid} ' not in listed()) + return code + + def upgraded(name, nine, session): + """An Upgrade to the same build: the session exec'd under its pid, its + 9P again.""" + nine.write('/ctl', b'Upgrade\n') + wait_for('the session exec\'d', lambda: any(a.startswith(b'--upgraded=') for a in cmdline(session)), timeout=60) + nine.socket.close() + again = reconnect(root / f'pardes-9p-{name}.sock') + assert alive(session) + return again + try: + # Kept from birth: `new --detach`. Its clients end and it runs on. + out = subprocess.run([binary, 'new', '--detach=kd', str(notes)], env=env, cwd=root, capture_output=True, + timeout=30, stdin=subprocess.DEVNULL) + assert out.returncode == 0, out + with Client(root / 'pardes-9p-kd.sock') as kn: + kd = int(dict(line.split(maxsplit=1) for line in kn.read('/status').decode().splitlines())['pid']) + assert state_of('kd') == '(tty, kept)', listed() + for how in ('HUP', 'TERM'): + ended_by(how, *attached('kd')) + assert alive(kd) and state_of('kd') == '(tty, kept)', (how, listed()) + 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') + # 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) + assert state_of('rows') == '(tty)', listed() + nine = upgraded('rows', nine, session) + wait_for('ps after the Upgrade', lambda: state_of('rows') == '(tty)') + wait_for('the client joined again', lambda: b'joined again' in t.text(), timeout=30) typed_detach(t, nine, pane) assert c.wait(timeout=20) == 0, c.returncode wait_for('the detach said', lambda: b"pardes: detached from 'rows', which is still running; come back with `pardes attach rows`" in t.text()) - assert alive(session) and f'session rows {session} (tty, detached)' in listed() - print('pair rows: Detach in the client: said, exit 0; the session runs on, listed') + assert alive(session) and state_of('rows') == '(tty, kept)', listed() + nine = upgraded('rows', nine, session) + wait_for('ps after the Upgrade', lambda: state_of('rows') == '(tty, kept)') + print('pair rows: a pair (ps: (tty)), so after an Upgrade; Detach in its client: said, exit 0, it runs on, kept, so after an Upgrade') for how in ('TERM', 'KILL', 'HUP'): - tt = Term(30, 100) - terms.append(tt) - a = tt.spawn([binary, 'attach', 'rows'], env) - drawn(tt) - wait_for('the client listed', lambda: f'client {a.pid} ' in listed()) - if how == 'HUP': - tt.close() - terms.remove(tt) - else: - os.kill(a.pid, getattr(signal, 'SIG' + how)) - a.wait(timeout=20) - if how == 'TERM': - wait_for('its terminal put back', lambda: tt.text().rfind(b'\x1b[?1049l') > tt.text().rfind(b'\x1b[?1049h')) - wait_for('the client unlisted', lambda: f'client {a.pid} ' not in listed()) - assert alive(session), how - print(f'pair rows: a client ended by {how}: the session runs on') + ended_by(how, *attached('rows')) + assert alive(session) and state_of('rows') == '(tty, kept)', (how, listed()) + print(f'pair rows: kept: a client ended by {how}: the session runs on') nine.socket.close() assert subprocess.run([binary, 'kill', 'rows'], env=env, capture_output=True, timeout=30).returncode == 0 wait_for('the session ended', lambda: not alive(session), timeout=30) @@ -2298,7 +2345,7 @@ def gui_pair(gui_binary): try: w, nine, session, pane = started('gp', env) - wait_for('the window listed its client', lambda: f'session gp {session} (tty, detached)' in listed() + wait_for('the window listed its client', lambda: f'session gp {session} (tty)' in listed() and f'client {w.pid} ' in listed()) w.keys(b'Ofrom-the-window\x1b') wait_for('its keys in the session\'s text', lambda: b'from-the-window' in nine.read(f'/pane/{pane}/body')) @@ -2316,11 +2363,29 @@ def gui_pair(gui_binary): os.kill(w.pid, signal.SIGKILL) w.p.wait(timeout=10) wait_for('the client unlisted', lambda: f'client {w.pid} ' not in listed()) - assert alive(session) and f'session gl {session} (tty, detached)' in listed(), listed() + assert alive(session) and f'session gl {session} (tty)' in listed(), listed() nine.socket.close() assert subprocess.run([binary, 'kill', 'gl'], env=env, capture_output=True, timeout=30).returncode == 0 wait_for('the session ended', lambda: not alive(session), timeout=30) print('pair window: the window killed: the session runs on, listed (a loss); killed by name') + # Kept from birth (`new --detach`): its window client's close leaves it. + out = subprocess.run([binary, 'new', '--detach=gk', str(notes)], env=env, cwd=root, capture_output=True, + timeout=30, stdin=subprocess.DEVNULL) + assert out.returncode == 0, out + with Client(root / 'pardes-9p-gk.sock') as kn: + gk = int(dict(line.split(maxsplit=1) for line in kn.read('/status').decode().splitlines())['pid']) + assert f'session gk {gk} (tty, kept)' in listed(), listed() + w = Window(root, 'gkw', [gui_binary, 'attach', 'gk'], env) + windows.append(w) + wait_for('the window client listed', lambda: f'client {w.pid} ' in listed(), timeout=30) + w.p.stdin.write(b'\x1b]777;close\x07') + w.p.stdin.flush() + assert w.p.wait(timeout=30) == 0, (w.p.returncode, w.said()[-400:]) + wait_for('the client unlisted', lambda: f'client {w.pid} ' not in listed()) + assert alive(gk) and f'session gk {gk} (tty, kept)' in listed(), listed() + assert subprocess.run([binary, 'kill', 'gk'], env=env, capture_output=True, timeout=30).returncode == 0 + wait_for('the session ended', lambda: not alive(gk), timeout=30) + print('pair window: a `new --detach` session (kept): its window client closed, it runs on') w, nine, session, pane = started('gn', dict(env, PARDES_DAEMON='off')) nine.socket.close() os.kill(w.pid, signal.SIGKILL) -- cgit v1.3 From aa05797120cef3cd883ce053d56ba00fe866c009 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 20 Nov 2026 14:52:50 -0300 Subject: pair: a hang-up is a close (P36): a terminal client (`runClient`) blocks HUP and TERM before its join makes a thread and winchWatch takes both as `quit`, which in a client now sets the goodbye's close (as a window's close button does, and as SDL already makes a window client's TERM), so its terminal closed, its ssh dropped under it or a TERM says goodbye with the close, puts the terminal back and exits 0, and the session, a pair's not kept (daemon.State `pair`), ends when that was its last client; with other clients or kept it runs on, and a client killed outright stays a loss (no goodbye: it runs on, listed); without PARDES_PAIR the close is read by no session, so the own path's terminal closed still detaches its session, the one change a user sees there being a `pardes attach` client's TERM: a goodbye and exit 0 where it died of the signal; and a client exec'd at an Upgrade (runtime/client.zig `execAgain`) clears its signal mask first, since the mask crosses an exec and a build that sigwaits no TERM would never take one (found by `--upgrade` new->old with clients: the exec'd terminal client ignored the test's TERM for 20 s; with the clear both ways pass); test/daemon.py `pair_rows` gains 2.2 B's not-kept column (a client's terminal closed, not the last: runs on; the pair's own client killed -9: runs on, listed `(tty)`; the last client TERMed: exit 0, its terminal put back, the session ended; a pair upgraded, its terminal closed: ended) and the own path's rows (an attach client TERMed: the session on; its terminal closed: `(tty, detached)`); on the change before, `--pair` fails at the TERMed client's status (-15); here: tui core-test 861/1016 (155 skipped), shell-test 1058/1061, term 17/17, pane 183/184, kitty 45/45, behaviour-test and --replay 275/275, process-test 0 of 144 failed, soak --clients 5 diverged 0, unequal 0, checked/wrong 1874/0 1937/0, quiet checks 597/0 673/0 779/0, unlisted 0, corpus 22 of 22, no leak, --bytes --clients 5 the same (quiet checks 693/0 586/0 802/0), daemon.py plain (launch, pair, pair_rows), --far, --relink, --manage, --window-clients, --gui (gui_pair) pass, an idle terminal pair wakes 0 and 0 times in 2 s, gui unit-test 1654/1658 with only BaselineHarnessMismatch, --games, --upgrade with clients both ways against the base built with -Dembed-sources=true (another semantics) pass, nothing left behind by any mode (load 4-18) Co-Authored-By: Claude Opus Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- dev/tui/tui.zig | 23 +++++++++++++++-------- runtime/client.zig | 5 +++++ test/daemon.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 8 deletions(-) (limited to 'test/daemon.py') 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) -- cgit v1.3