From 8caed8e7d894fee836c95f2ca83fda9b22e1db84 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Sun, 22 Nov 2026 14:01:03 -0300 Subject: replica: a client's own join again is judged after its goodbye (pardes-term-join-9, item 1 (a)): a client let go for its lag or after a divergence says bye, shuts its link and dials again naming its view (runtime/client.zig `run`), and the session takes its join from the daemon's hand-over and its bye from the old link's reader thread, in no order between them; judged first, `viewFor` found the view not away and opened a NEW one (the client's layout, scroll and focus lost, its old view then kept away 30 s for nobody), or at the cap refused it (a divergence's rejoin is not patient and then fails "could not join again"); now a join naming a view whose link has hung up (`hungUp`: POLLHUP or POLLRDHUP, the client having shut its link before it dialled) is held on that peer (`Peer.rejoin`) and posted again when the link is let go (`letGo`), its view away by then, a view whose link is up being another client's as before, and a Restore keeps a held join as it keeps the inbox's; measured with a raw replica that says bye and at once joins again naming its view, pinned to one CPU: the base 1 of 60 in another view and 27 refused, this 0 of 120; the cut-off test dials again before its old link's end is heard (fails on the base with `error.NotHeld`); root.zig's machine_lines for replica.zig 73 -> 76 (the session's own join, no replayed step); not covered: a relay that delays each connection on its own (test/delay_relay.py) can still bring the hello before the old link's hang-up Co-Authored-By: Claude Opus Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- core/replica.zig | 46 +++++++++++++++++++++++++++++++++++++++------- core/root.zig | 2 +- 2 files changed, 40 insertions(+), 8 deletions(-) diff --git a/core/replica.zig b/core/replica.zig index e9e29de0..ae1ae6c3 100644 --- a/core/replica.zig +++ b/core/replica.zig @@ -1379,6 +1379,9 @@ pub const Peer = struct { /// Cut off (`send`): the words why are the last it is sent, and its /// view is kept for it (views.away), as for a bye's `again`. cut: bool = false, + /// Its client's join again for its view, come before this link's end + /// was heard: judged once it is let go (`Peers.join`, `letGo`). + rejoin: ?Peers.Joining = null, /// Its writer has ended (what it was sent, written). done: std.atomic.Value(bool) = .init(false), /// Its join, which its writer writes before what it is sent: the @@ -1722,6 +1725,13 @@ pub fn dying() void { }; } +/// Whether the other end of socket `fd` has hung up (shut, or closed). +fn hungUp(fd: c_int) bool { + const rdhup = if (@hasDecl(libc.POLL, "RDHUP")) libc.POLL.RDHUP else 0; + var p = [1]libc.pollfd{.{ .fd = fd, .events = libc.POLL.IN | rdhup, .revents = 0 }}; + return libc.poll(&p, 1, 0) == 1 and p[0].revents & (libc.POLL.HUP | rdhup) != 0; +} + /// The process at the other end of unix socket `fd` (Linux's SO_PEERCRED), /// or 0: a client's through ssh is ssh's. fn peerPid(fd: c_int) libc.pid_t { @@ -1905,6 +1915,16 @@ pub const Peers = struct { /// threads started. At a step boundary (`boundary`), or a session's /// first client before its first effects (runtime/session.zig). pub fn join(ps: *Peers, core: *Pardes, joining: Joining) void { + // A join for a view whose client's link has hung up is that client + // joining again (runtime/client.zig `run`: its bye, its link shut, + // then its dial): its goodbye comes on that link and this join + // through the daemon, in no order between them. Held until that + // link is let go, its view kept for it by then; a view whose link + // is up is another client's. + if (joining.view) |v| for (ps.list) |slot| if (slot) |p| if (p.view.index() == v and p.rejoin == null and hungUp(p.fd)) { + p.rejoin = joining; + return; + }; const fd = joining.fd; const t0 = nowNs(); defer { @@ -2106,6 +2126,7 @@ pub const Peers = struct { _ = ps.inbox.orderedRemove(i); } ps.lock.unlock(io); + if (peer.rejoin) |j| ps.post(.{ .join = j }); ps.gpa.destroy(peer); } @@ -2157,6 +2178,10 @@ pub const Peers = struct { _ = ps.inbox.orderedRemove(i); } ps.lock.unlock(ps.io); + for (ps.list) |slot| if (slot) |p| if (p.rejoin) |j| { + again.append(ps.gpa, j) catch continue; + p.rejoin = null; + }; ps.drop(core, false); ps.restored = true; for (again.items) |j| ps.post(.{ .join = j }); @@ -5007,13 +5032,21 @@ test "a client cut off for what it left unread is told why and joins again for i // welcome, the dump with its blobs in place); and what it queued for one, // down its link in order, the link's end after it if it was cut off. const MemoryLinks = struct { - fn join(ps: *Peers, core: *Pardes, view: ?u8, fd: *c_int) !struct { r: *Replica, peer: *Peer } { + /// `old`: the link it had, whose end the session hears after this + /// join came: held until then. + fn join(ps: *Peers, core: *Pardes, view: ?u8, fd: *c_int, old: ?*Peer) !struct { r: *Replica, peer: *Peer } { const gpa = std.testing.allocator; var fds: [2]c_int = undefined; if (libc.socketpair(libc.AF.UNIX, libc.SOCK.STREAM | libc.SOCK.CLOEXEC, 0, &fds) != 0) return error.NoSocket; fd.* = fds[1]; ps.handed(fds[0], view); ps.boundary(core); + if (old) |o| { + for (ps.list) |slot| if (slot) |p| if (p.fd == fds[0]) return error.NotHeld; + ps.post(.{ .gone = o }); + ps.boundary(core); + ps.boundary(core); + } const peer = for (ps.list) |slot| { if (slot) |p| if (p.fd == fds[0]) break p; } else return error.Refused; @@ -5061,7 +5094,7 @@ test "a client cut off for what it left unread is told why and joins again for i defer ps.deinit(); defer ps.dropAll(core); var fd: c_int = -1; - const first = try MemoryLinks.join(&ps, core, null, &fd); + const first = try MemoryLinks.join(&ps, core, null, &fd, null); defer _ = libc.close(fd); var link: Link = .{ .r = first.r, .fd = fd }; defer link.deinit(gpa); @@ -5088,13 +5121,12 @@ test "a client cut off for what it left unread is told why and joins again for i // It was told why, and joins again (as a divergence's client does)... try std.testing.expect(link.rejoins()); try std.testing.expect(std.mem.indexOf(u8, link.notice(), "behind") != null); - // ...once its old link's end is heard: its view kept for it meanwhile. - ps.post(.{ .gone = first.peer }); - ps.boundary(core); - try std.testing.expect(pardes.views.isAway(core, view.index())); + // ...its old link shut, at once (runtime/client.zig `run`): a join that + // comes before that link's end is heard, on another connection, waits + // for it, its view kept for it meanwhile. first.r.deinit(); _ = libc.close(fd); - const back = try MemoryLinks.join(&ps, core, view.index(), &fd); + const back = try MemoryLinks.join(&ps, core, view.index(), &fd, first.peer); defer back.r.deinit(); try std.testing.expectEqual(view, back.peer.view); link.reset(back.r, &.{ .fd = fd, .pid = 0, .client = back.r.id, .view = view.index(), .gmtoff = 0, .dump = &.{} }); diff --git a/core/root.zig b/core/root.zig index 0e02a9bc..fb606909 100644 --- a/core/root.zig +++ b/core/root.zig @@ -632,7 +632,7 @@ const machine_lines = [_]struct { []const u8, usize, []const u8 }{ .{ "jj.zig", 11, "jj run under tape.site(.jj) on an io of its own (the tape's spawns nothing) with this process's environment, the signal mask cleared as Stream's children's: the session's, a replica replays its answer; or on a worker for `Changes`, its answer an input, with the wall clock it was made at (tape.value(.clock) on the loop) for its descriptions' ages; a test's shell inbox (dev)" }, .{ "look.zig", 6, "openLink's fork (the host's: a view's frontend opens a link) and a remote's error name" }, .{ "memory.zig", 7, "the memory regions themselves (mmap, madvise, their locks)" }, - .{ "replica.zig", 73, "the client and session engines: sockets, threads, clocks, the blob cache, traces" }, + .{ "replica.zig", 76, "the client and session engines: sockets, threads, clocks, the blob cache, traces" }, .{ "rollback.zig", 1, "a test helper's directory" }, .{ "root.zig", 4, "dev's modules re-exported (host_io, file_watch, glide, fonts)" }, .{ "selection_pipe.zig", 14, "a pipe's child processes and their files: the host's `pipe` entry" }, -- cgit v1.3