From a3d98f0e465ed9634d0534e0a6c71767b2a709b9 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Mon, 23 Nov 2026 00:05:38 -0300 Subject: link: a client whose link is up and must take the session's state again (it diverged, fell 2 s behind, or the session cut it off 64 MB behind) comes back OVER THAT LINK, the daemon not asked: its goodbye that says `again` and today's hello for its view in one write (`replica.comeBack`, runtime/client.zig `Loop.run` when `Link.backHere`), its reader letting the stream it leaves go (units, blobs and checks not even copied) and stopping at the session's welcome there, put back at its framer's front for the join's reader (`readLink`'s back mode, `Framer.putBack`), the join read on that framer (`joinOn` takes it; `replica.join`'s `here`); on the session the reader reads on after a `bye` that says `again`, a hello after it is the same client back (`Peer.take`, inputs numbered afresh), and `Peers.joinAgain` dumps for it there with what the old road put on the tape (`awayView`, then the join's `viewFor`: every side's terminals reloaded in that step), what was queued for it let go, the cut over; one writer loop writes a join whenever one is due (`Peer.gen`, `Peer.written`; the `.joined` note carries its gen so a stale one releases nothing), what it was sent a MiB at most a write at a message's end (`writeOut`) so a join due waits for no more of the old stream, a far client's old zstd frame ended before the welcome; a cut keeps the link (its words, then nothing until the hello); a link that ends before the welcome (an Upgrade's or a Restore's words: the session closes it), or one gone, is dialled again as before: the one case left for a redial; and a client refused its own load (`fromDump`) asks ONCE more over the same link with the session's digest a word a field after that dump (wire 31, nothing at 31 installed: a bye's byte 3 = `again` + `parts`, a new ServerTag `parts` 0x0d) and only then gives up, naming the fields that differ in its words and its log; the failing run first: test/daemon.py's new `pair_back` (a no-daemon pair whose client diverges, and one stopped 3 s while the session is edited) on the installed build 79b73e14711e: "the client and the session went (client exit 1)", "'diverged' let this client go", on this tree both join again over their own link, the session alive, the unsaved text there, agreed; `clients`: client D's socket the same across its rejoin, the session counting 6 joins, 1 over a client's own link and 5 handed by the daemon; `far_flood` (20 KB/s, 200 ms): the same link after 3 joins again of 0.56 s each against a dial each time on the installed build (0.99 s each), but agreement 68.6 s after `yes` ended against 22.7 s: the client reads through what the relay holds for it (the relay buffers without bound; ssh bounds it by its window), said in reports/pardes-link-lifecycle.md; `Peer.rejoin` and `hungUp` stay for the one road that still dials after a goodbye saying `again` (a client of another build exec'ing: `sameBuild`), their test rewritten for it; core tests at the replica's surface with real link threads (near and far: back over its own link; a refused load asked once more and loaded; refused twice and named, against a session played by hand; a cut client back over its link, the same peer, connection and view); the soak models the road (`ended`, `Soak.joinDown`, the `here` column); core-test 924/1084 (160 skipped), shell-test 1126/1129 (3 skipped), root.zig's machine_lines for replica.zig 76 -> 90 (the tests' sockets and sleeps; the session's one shutdown of a link it will not dump for again, and a close) Co-Authored-By: Claude Opus Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- runtime/client.zig | 45 +++++++++++++++++++++++++++++++++------------ 1 file changed, 33 insertions(+), 12 deletions(-) (limited to 'runtime/client.zig') diff --git a/runtime/client.zig b/runtime/client.zig index 43fdf98d..493f44b7 100644 --- a/runtime/client.zig +++ b/runtime/client.zig @@ -95,7 +95,7 @@ pub const Start = struct { s.o.frames = &s.frames; // Exec'd at an Upgrade (`execAgain`): for the view it had. if (init.environ_map.get("PARDES_JOIN_VIEW")) |v| s.o.join_view = std.fmt.parseInt(u8, v, 10) catch null; - s.r = replica.join(s.gpa, &s.joined, dial.path, dial.session, s.o, s.have, &s.why, null) orelse return false; + s.r = replica.join(s.gpa, &s.joined, dial.path, dial.session, s.o, s.have, &s.why, null, false) orelse return false; return true; } @@ -209,11 +209,23 @@ pub fn Loop(comptime Frontend: type) type { l.after(); } if (s.env.get("PARDES_REPLICA_STATS")) |file| link.writeStats(file); - if (!link.gone) replica.bye(s.joined.fd, .{ .again = s.r.diverged != null or link.lagged != 0, .close = link.close }); - // The link's reader ends with the link, posting as it goes: - // what it posted is let go until it has (a terminal's queue - // is bounded: a reader blocked on it would never end). - _ = libc.shutdown(s.joined.fd, 2); + // Its link up and the session's state to take again: its + // goodbye and its hello said on it, the session's dump comes + // there (`replica.comeBack`); its reader reads on to the welcome, + // letting the stream it leaves go. Only a link that is gone + // is dialled again. + const here = link.backHere() and s.joined.rest.overlong() == null and back: { + link.back.store(true, .release); + break :back replica.comeBack(s.joined.fd, s.dial.session, s.r.view, false); + }; + if (!here) { + if (!link.gone) replica.bye(s.joined.fd, .{ .again = s.r.diverged != null or link.lagged != 0, .close = link.close }); + _ = libc.shutdown(s.joined.fd, 2); + } + // The link's reader ends with the link (or at the welcome), + // posting as it goes: what it posted is let go until it has + // (a terminal's queue is bounded: a reader blocked on it + // would never end). while (true) { const ended = done.load(.acquire); l.fe.drop(); @@ -221,14 +233,23 @@ pub fn Loop(comptime Frontend: type) type { std.Thread.yield() catch {}; } reader.join(); - _ = libc.close(s.joined.fd); - s.joined.rest.deinit(s.gpa); + if (!here) { + _ = libc.close(s.joined.fd); + s.joined.rest.deinit(s.gpa); + } // Its link cut with no word by a session that is there still // (an Upgrade's exec): joined again, as when told so. if (link.gone and link.refused == null and link.ended == null and !link.left and replica.cutListed("the session hung up", s.dial.session, s.have != null)) link.refused = std.heap.c_allocator.dupe(u8, replica.upgrading) catch null; - // Why it joins again is in its notice until a key. - if (!link.rejoins()) { + // A last word goes first, one read after it said it comes + // back too (a Detach in it, the session's end). Why it joins + // again is in its notice until a key. + const goes = link.ended != null or link.left; + if (here and goes) { + _ = libc.close(s.joined.fd); + s.joined.rest.deinit(s.gpa); + } + if (goes or !link.rejoins()) { // What it made since it joined, kept for its next start. if (s.have) |h| h.keep(s.r.core()); s.r.deinit(); @@ -238,7 +259,7 @@ pub fn Loop(comptime Frontend: type) type { } // The session again, the core it leaves held meanwhile: its // blobs are not sent again. - s.r = replica.join(s.gpa, &s.joined, s.dial.path, s.dial.session, s.o, s.have, &s.why, s.r) orelse { + s.r = replica.join(s.gpa, &s.joined, s.dial.path, s.dial.session, s.o, s.have, &s.why, s.r, here) orelse { if (replica.ended(&s.why)) return .gone; if (replica.otherBuild(&s.why) and upgrade.replaced()) return .{ .upgrade = s.joined.view }; return .failed; @@ -272,7 +293,7 @@ pub fn Loop(comptime Frontend: type) type { /// and its end. fn read(l: *L, s: *Start, done: *std.atomic.Value(bool)) void { defer done.store(true, .release); - replica.readLink(s.gpa, s.joined.fd, &s.joined.rest, l.fe, Frontend.said); + replica.readLink(s.gpa, s.joined.fd, &s.joined.rest, &l.link, l.fe, Frontend.said); l.fe.ended(); } -- cgit v1.3 From 396247178d20678a36e9e1782c2a457eeb1916de Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Mon, 23 Nov 2026 04:21:13 -0300 Subject: link: a far client that falls behind dials again, as before round 1 (the coordinator's ruling P63a): `Link.backHere` says no for a link that crosses machines (`Link.far`, set by runtime/client.zig from its `Have`) and fell behind `behind_max_ms`, so that client says its goodbye, shuts its link and dials again through the daemon, a new connection that drops what the old one holds for it; every other way back (a divergence, a near lag, a cut, a refused load's ask) stays on the link; marked in the code as the second road it is, with what replaces it (bounded flight: the session never more than a window past what its client has taken); the failing row first: test/daemon.py `--far` on ReleaseSafe 6 of 7 on round 1's tree (`far_flood`: over its own link the far client read through all the relay held for it, agreeing 49-68 s after `yes` against 22.7 s by a dial); `far_flood` now asserts the far client behind took ANOTHER connection; the lag test asserts both roads (`backHere` near yes, far no) ; gated with the next change as its tree (g1, rnllpvzw fd3730201cb3): `--far` on ReleaseSafe 7 of 7, far_flood ANOTHER connection each time and agreed 22.3-26.0 s after `yes` ended (Debug 25.3 s), every other row as before: core-test 931/1095 (164 skipped), shell-test 1137/1140 (3 skipped), process-test 0 of 146 failed both ways, the soak x3 and --pair x3 clean, daemon.py every mode on both builds, the upgrades 12 of 12, frames 77/77 and 121 counts within 0.1% against tree6, the cross-builds Co-Authored-By: Claude Opus Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- core/replica.zig | 16 ++++++++++++++++ runtime/client.zig | 2 +- test/daemon.py | 8 +++++--- 3 files changed, 22 insertions(+), 4 deletions(-) (limited to 'runtime/client.zig') diff --git a/core/replica.zig b/core/replica.zig index 448c428f..79abd977 100644 --- a/core/replica.zig +++ b/core/replica.zig @@ -3292,6 +3292,9 @@ pub const Link = struct { fresh: bool = true, /// How far behind it was as it let its link go (`rejoins` says so). lagged: u64 = 0, + /// Its link crosses machines (a `Have`: ssh's forward, a relay): fallen + /// behind, it dials again (`backHere`). + far: bool = false, /// A pair's first join, its own boot not confirmed yet: its inputs /// kept here, not sent (`release`; client.zig `confirm`). held: ?std.ArrayListUnmanaged(u8) = null, @@ -3526,7 +3529,15 @@ pub const Link = struct { /// a cut's keep the link, an Upgrade's and a Restore's end it, which the /// join there finds): its goodbye and hello are said on it (`comeBack`). /// One that is gone, or a last word, is not. + /// THE SECOND ROAD: a far client that fell behind dials again (a new + /// connection; the one it leaves is shut): over its own link it would + /// first read through all that link holds for it, which a relay or a + /// forward buffers without bound (test/daemon.py `far_flood`: 49-68 s + /// to agree after the flood against 22.7 s by a dial). Bounded flight + /// (the session never more than a window past what its client has + /// taken) replaces it, and this line goes. pub fn backHere(c: *const Link) bool { + if (c.far and c.lagged != 0) return false; return !c.gone and c.ended == null and !c.left and (c.r.diverged != null or c.lagged != 0 or c.refused != null); } @@ -6053,6 +6064,11 @@ test "a client whose session's units come more than two seconds later than its q // core is behind on purpose, no divergence. link.take(.{ .checked = digestOf(a) +% 1 }); try std.testing.expect(r.diverged == null); + // It comes back over its link; far, it dials again (`backHere`). + try std.testing.expect(link.backHere()); + link.far = true; + try std.testing.expect(!link.backHere()); + link.far = false; try std.testing.expect(link.rejoins()); try std.testing.expectEqualStrings("joined again: 2.1 s behind", link.notice()); try std.testing.expect(!link.rejoins()); diff --git a/runtime/client.zig b/runtime/client.zig index 2fbc75c8..1d348aea 100644 --- a/runtime/client.zig +++ b/runtime/client.zig @@ -175,7 +175,7 @@ pub fn Loop(comptime Frontend: type) type { frames: *pardes.FrameStats, pub fn init(fe: *Frontend, s: *Start) !L { - var l: L = .{ .link = .{ .r = s.r, .fd = s.joined.fd }, .fe = fe, .frames = &s.frames }; + var l: L = .{ .link = .{ .r = s.r, .fd = s.joined.fd, .far = s.have != null }, .fe = fe, .frames = &s.frames }; try l.link.fromEnv(s.gpa, s.env); return l; } diff --git a/test/daemon.py b/test/daemon.py index c22d95d4..caa1272b 100644 --- a/test/daemon.py +++ b/test/daemon.py @@ -2382,13 +2382,15 @@ def far_flood(binary): # Queued behind the flood, let go for its lag, joined again from a # dump: a link of its ended (its stats line), and its notice says why. assert joins(stats), ('no link of the far client ended under the flood', checks(stats)) - # Over its own link (the relay's one connection for it), each join's - # time and the KB it was sent; the flood's end to its agreement. + # Fallen behind, a far client dials again (core/replica.zig + # `Link.backHere`: the second road until bounded flight): a new + # connection, not what the old one holds for it read through; each + # join's time and the KB it was sent; the flood's end to its agreement. same = sockets(c.pid) == links print(f'far flood: the far client\'s link {"the same" if same else "another"} after {len(joins(stats))} joins again ' f'(join ms {[j.get("join_ms") for j in joins(stats)]}, sent KB {[j.get("sent_kb") for j in joins(stats)]}); ' f'agreed {time.monotonic() - ended_at:.1f} s after yes ended') - assert same, ('the far client joined again over its own link', links, sockets(c.pid)) + assert not same, ('the far client behind dialled again: another connection', links, sockets(c.pid)) wait_for('the far client\'s notice of its join again', lambda: b'joined again: ' in t.text() and b' s behind' in t.text(), timeout=30) os.write(t.master, b'echo from-far\r') wait_for('the far client\'s keys in the shell', lambda: b'from-far' in nine.read(f'/pane/{shell}/body'), timeout=30) -- cgit v1.3