From ae3b9e9d19492c827f257da8a224a19d77de51c1 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 20 Nov 2026 05:58:08 -0300 Subject: last word: a session's last frame on a link says which of two things happened (wire `ended CODE` 0x0b and `left NAME` 0x0c, working names): a Detach typed in a client queues `left` and lets its writer end after it (Peers.detach, Peer.last), a session's end tells every client `ended` with its code before the links are let go (Peers.ended, from runtime/session.zig after the loop; refuseAll and it share the 200 ms flush); a client that typed Detach prints "pardes: detached from 'NAME', which is still running; come back with `pardes attach NAME`" and exits 0 again (the attach's regression to exit 1 and "let this client go" undone, for every client, no variable), a client whose session ended exits with the session's code quietly, a pair's client after reaping its session (EDITOR=pardes waits for the session's end); client.Ended a union (left, ended CODE); a Detach with no daemon is refused in a client too (tui.zig `detach`: the daemon checked first), since a pair with none ends with its last link; test/daemon.py: the client Detach expectations (0 and the line), a session's Exit (0, no let-go line), `pair` Exit 0 and the session reaped, `pair_rows` the rows of the design's tables 1.1 and 2.2 B (Detach said, exit 0, the session listed; a client ended by TERM with its terminal put back, by KILL and by its terminal's HUP: the session runs on; no daemon: Detach refused, the terminal closed ends the session; nothing left); docs/next-run.md and core/CHANGELOG.md say so (the CHANGELOG's `new --detach` line is the first change's); `kept` NOT built: no terminal behaviour turns on it while a hangup and a killed client are losses and Exit ends the session (see the report) Co-Authored-By: Claude Opus Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- core/replica.zig | 64 ++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 48 insertions(+), 16 deletions(-) (limited to 'core/replica.zig') diff --git a/core/replica.zig b/core/replica.zig index bf665a50..e1eacf6c 100644 --- a/core/replica.zig +++ b/core/replica.zig @@ -1887,6 +1887,16 @@ pub const Peer = struct { peer.cond.signal(io); } + /// Its last message: queued, and its writer ends once it is written. + fn last(peer: *Peer, words: []const u8) void { + const io = peer.peers.io; + peer.send(&.{words}); + peer.lock.lockUncancelable(io); + peer.closing = true; + peer.cond.signal(io); + peer.lock.unlock(io); + } + fn writeLoop(peer: *Peer) void { const io = peer.peers.io; defer peer.done.store(true, .release); @@ -2404,14 +2414,14 @@ pub const Peers = struct { if (ps.count() == 0) return; var out: [600]u8 = undefined; const words = wire.encodeServer(&out, .{ .refused = why }) catch return; - const io = ps.io; - for (ps.list) |slot| if (slot) |peer| { - peer.send(&.{words}); - peer.lock.lockUncancelable(io); - peer.closing = true; - peer.cond.signal(io); - peer.lock.unlock(io); - }; + for (ps.list) |slot| if (slot) |peer| peer.last(words); + ps.flushed200(); + // Each joins again for its view, kept in the dump (views.away). + ps.drop(core, true); + } + + /// What each client was sent goes first, for up to 200 ms. + fn flushed200(ps: *Peers) void { const until = nowNs() + 200 * std.time.ns_per_ms; while (nowNs() < until) { const busy = for (ps.list) |slot| { @@ -2421,8 +2431,6 @@ pub const Peers = struct { const ts: libc.timespec = .{ .sec = 0, .nsec = 2 * std.time.ns_per_ms }; _ = libc.nanosleep(&ts, null); } - // Each joins again for its view, kept in the dump (views.away). - ps.drop(core, true); } /// A join's dump and pins let go: written, or its client gone. @@ -2445,16 +2453,26 @@ pub const Peers = struct { daemon.clients(pids[0..n]); } - /// Detach typed in client view `k`'s: its link ends (its reader says - /// it is gone, and the next boundary lets it go). + /// Detach typed in client view `k`'s: told it left the session running + /// (`left`), its last word; the client goes, and its reader says so. pub fn detach(ps: *Peers, k: u8) bool { for (ps.list) |slot| if (slot) |peer| if (peer.view == k) { - _ = libc.shutdown(peer.fd, 2); + var out: [wire.max_name + 8]u8 = undefined; + peer.last(wire.encodeServer(&out, .{ .left = ps.name[0..@min(ps.name.len, wire.max_name)] }) catch unreachable); return true; }; return false; } + /// The session ends with `code`: every client told so (`ended`), its + /// last word, before the links are let go (`dropAll`). + pub fn ended(ps: *Peers, code: u8) void { + var out: [16]u8 = undefined; + const words = wire.encodeServer(&out, .{ .ended = code }) catch unreachable; + for (ps.list) |slot| if (slot) |peer| peer.last(words); + ps.flushed200(); + } + fn stop(ps: *Peers, core: *Pardes) void { if (!ps.recording) return; pardes.panes.resizeDeferred(core); @@ -2919,7 +2937,7 @@ pub fn bye(fd: c_int, again: bool) void { /// What the session streams a client after its dump, as its frontend's loop /// takes it: a unit's and a terminal message's bytes and a refusal's words /// on the C heap, the loop's to free. -pub const Said = union(enum) { unit: []u8, screen: []u8, checked: u64, refused: []u8, blob: []u8 }; +pub const Said = union(enum) { unit: []u8, screen: []u8, checked: u64, refused: []u8, blob: []u8, ended: u8, left }; /// The session's stream off `fd`, each message handed to `post` until the /// link ends (a refusal ends it too); `post` says whether it took it (one @@ -2934,13 +2952,16 @@ pub fn readLink(gpa: Allocator, fd: c_int, rest: *Framer, ctx: anytype, comptime .blob => .{ .blob = std.heap.c_allocator.dupe(u8, payload) catch return false }, .check => .{ .checked = (wire.decodeServer(tag, payload) catch return false).check }, .refused => .{ .refused = std.heap.c_allocator.dupe(u8, (wire.decodeServer(tag, payload) catch return false).refused) catch return false }, + .ended => .{ .ended = (wire.decodeServer(tag, payload) catch return false).ended }, + .left => .left, .pong, .welcome, .dump, .blobs, .zstd => return true, }; if (!post(x, msg)) switch (msg) { .unit, .screen, .refused, .blob => |b| std.heap.c_allocator.free(b), - .checked => {}, + .checked, .ended, .left => {}, }; - return msg != .refused; + // A last word ends the stream. + return msg == .unit or msg == .screen or msg == .checked or msg == .blob; } }; rest.run(gpa, fd, ctx, Take.take); @@ -3078,6 +3099,9 @@ pub const Link = struct { visible: u32 = 0, /// What the session said as it let this client go (the C heap's). refused: ?[]u8 = null, + /// Its last word: it ended, with this code; or this client left it. + ended: ?u8 = null, + left: bool = false, /// A line over its frame, until its next key (`notify`): why it joined /// again. The client's own, never the session's core. notice_buf: [240]u8 = undefined, @@ -3196,6 +3220,14 @@ pub const Link = struct { c.refused = words; c.quit = true; }, + .ended => |code| { + c.ended = code; + c.quit = true; + }, + .left => { + c.left = true; + c.quit = true; + }, } if (c.r.diverged != null) c.quit = true; } -- cgit v1.3