summaryrefslogtreecommitdiff
path: root/src/replica.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-10-30 15:19:25 -0300
committerGabriel Schneider <[email protected]>2026-10-30 17:03:40 -0300
commitf2ede98aee0265e3506f5afd6039c70be7812767 (patch)
tree284a64fc9fcf7ba777a20fad75cc6295d70695fb /src/replica.zig
parent08c9415dde1a1d5da2c20124c4fcd4c92783b290 (diff)
downloadpardes-f2ede98aee0265e3506f5afd6039c70be7812767.tar.gz
pardes-f2ede98aee0265e3506f5afd6039c70be7812767.zip
remote clients: a pointer's hit carries the place it was made on (Layer.Place, on the wire, version 17) and a step resolves it from that, never from this process's frame (`surface`, `.local`), which a session never draws for a client: a window's pointer over a tag diverged it at once (the user's `tape byte 119`, 15 rejoins in 25 s) and a click there aborted it; a replay that meets a site its tape lacks, or makes a syscall that is none, is a divergence that goes on harmless (poisonOf), not a panic, and a speculation that makes one waits (Tty's shell lookup, untaped, is a value site now, as Shell's was); a copy over a queue is refused, not asserted; a session's last client, while no one else watches it, leaves it its layout (leaveView, settled as closeView is), which its rejoin copies; a redial waits only after an Upgrade's `refused` (a divergence's redialled a gone session 20 s); a dying session shuts its clients' links before its trace, and an --attach client ends its ssh and socket whatever way it exits; a far join's hashing stops at a blob once its client is let go (its leave waits for it in the step); a confirmed speculation's state is checked against its unit's digest, and the trace gives a key's and a pointer's words; test/daemon.py: 200 far joins level, a window's pointer swept over tags and body, ssh stand-in clients refused and detached, a client of a killed session ending in 2 s; in-process tests for each
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Diffstat (limited to 'src/replica.zig')
-rw-r--r--src/replica.zig124
1 files changed, 119 insertions, 5 deletions
diff --git a/src/replica.zig b/src/replica.zig
index 22de375f..2af32cdb 100644
--- a/src/replica.zig
+++ b/src/replica.zig
@@ -235,6 +235,16 @@ pub const Authority = struct {
return .{ .view = k, .unit = try a.unit(0, a.seq) };
}
+ /// Client `client`'s view gone (its link ended), as its unit.
+ pub fn closeView(a: *Authority, client: u16) ![]const u8 {
+ std.debug.assert(a.batched == 0);
+ const was = Stepping.begin(&a.t, null);
+ a.core.closeView(a.view_of[client]);
+ was.end();
+ a.seq += 1;
+ return a.unit(0, a.seq);
+ }
+
/// One client's input, applied in the order it came, in its view: the
/// unit every replica takes.
pub fn take(a: *Authority, msg: []const u8) ![]const u8 {
@@ -687,7 +697,23 @@ pub const Replica = struct {
r.applied -= 1;
if (r.applied == 0) {
// Nothing speculative on top any more: the state is the
- // confirmed one.
+ // confirmed one, which its digest says. A step of its own
+ // that read what the session's did not (a frame's, a
+ // derived field's) shows here, at the input that did it,
+ // not at some later unit replayed.
+ if (lastDigest(u.tape)) |d| {
+ tape.checkDigest(&r.t, d);
+ if (r.t.diverged) |why| {
+ if (std.c.getenv("PARDES_REPLICA_TRACE") != null) {
+ var w: std.Io.Writer.Allocating = .init(r.gpa);
+ defer w.deinit();
+ w.writer.print("replica {d}: its input {d} came to another state than the session's: {s}\n", .{ r.id, u.seq, why }) catch {};
+ tape.describe(u.tape, &w.writer);
+ std.debug.print("{s}", .{w.written()});
+ }
+ return r.fail(why);
+ }
+ }
r.copy.release();
rollback.Epoch.select(null);
r.clearSince();
@@ -1320,6 +1346,9 @@ pub const Peer = struct {
var list: std.ArrayListUnmanaged(u8) = .empty;
defer list.deinit(gpa);
for (peer.blobs.items) |b| {
+ // Gone meanwhile: its leave waits for this thread in the
+ // session's step, so the hashing stops here.
+ if (peer.letting()) return false;
var entry: [blob_entry]u8 = undefined;
std.mem.writeInt(u64, entry[0..8], b.at, .little);
std.mem.writeInt(u64, entry[8..16], b.bytes.len, .little);
@@ -1350,6 +1379,14 @@ pub const Peer = struct {
return peer.z != null;
}
+ /// Whether it is being let go (or was cut off).
+ fn letting(peer: *Peer) bool {
+ const io = peer.peers.io;
+ peer.lock.lockUncancelable(io);
+ defer peer.lock.unlock(io);
+ return peer.closing or peer.cut;
+ }
+
/// One message into its stream's compressor.
fn zMessage(peer: *Peer, tag: wire.ServerTag, payload: []const u8) bool {
const head = wire.header(@backingInt(tag), payload.len);
@@ -1513,6 +1550,18 @@ pub const Peer = struct {
}
};
+/// The session's clients, for `dying`.
+var live_peers: ?*Peers = null;
+
+/// The process is panicking: each client's link shut first, so that it
+/// hears the end now, not when the trace is written (seconds, symbolized).
+pub fn dying() void {
+ const ps = live_peers orelse return;
+ for (ps.list) |slot| if (slot) |peer| {
+ _ = libc.shutdown(peer.fd, 2);
+ };
+}
+
pub const Peers = struct {
gpa: Allocator = std.heap.c_allocator,
io: std.Io = undefined,
@@ -1750,6 +1799,7 @@ pub const Peers = struct {
const slot = for (&ps.list) |*s| {
if (s.* == null) break s;
} else return refuse(fd, "that session has as many clients as it takes");
+ live_peers = ps;
if (!ps.recording) {
// A session recording to a file (PARDES_RECORD) has its tape.
if (tape.on != null) return refuse(fd, "that session records a tape of its own (PARDES_RECORD)");
@@ -1836,7 +1886,7 @@ pub const Peers = struct {
log.info("client {d} left '{s}'", .{ peer.id, ps.name });
ps.screens.dropPeer(peer.sid);
core.undrawn &= ~(@as(u8, 1) << @intCast(peer.view));
- core.closeView(peer.view);
+ ps.closeViewOf(core, peer);
if (ps.count() == 0) core.unattended = ps.alone_unattended;
ps.seq += 1;
ps.unit(0, ps.seq);
@@ -1844,6 +1894,13 @@ pub const Peers = struct {
if (ps.count() == 0) ps.stop(core);
}
+ /// A gone client's view closed (its slot let go already): the last one
+ /// of a session no one else watches leaves the session its layout, for
+ /// its rejoin (an Upgrade's, a diverged one's) to find.
+ fn closeViewOf(ps: *Peers, core: *Pardes, peer: *Peer) void {
+ if (ps.count() == 0 and ps.alone_unattended) core.leaveView(peer.view) else core.closeView(peer.view);
+ }
+
/// Its threads ended and joined, its link closed.
fn letGo(ps: *Peers, peer: *Peer) void {
const io = ps.io;
@@ -1949,7 +2006,7 @@ pub const Peers = struct {
for (&ps.list) |*slot| if (slot.*) |peer| {
slot.* = null;
core.undrawn &= ~(@as(u8, 1) << @intCast(peer.view));
- core.closeView(peer.view);
+ ps.closeViewOf(core, peer);
ps.screens.dropPeer(peer.sid);
ps.letGo(peer);
};
@@ -2770,7 +2827,10 @@ pub const Link = struct {
/// dialled every 100 ms for 20 s. A far client's (`have`) holds the
/// core it leaves meanwhile (`Have.leaving`): its blobs are not sent
/// again. Null, and why in `why`.
- pub fn redial(gpa: Allocator, path: []const u8, session: []const u8, why: *[600]u8, have: ?*Have) ?Joined {
+ /// `patient`: after an Upgrade's `refused` alone, while the session is
+ /// away a moment; after a divergence a session that will not answer is
+ /// gone, and the client says so at once (it froze 20 s, 2026-10-30).
+ pub fn redial(gpa: Allocator, path: []const u8, session: []const u8, why: *[600]u8, have: ?*Have, patient: bool) ?Joined {
var tries: u32 = 0;
while (true) : (tries += 1) {
why.* = @splat(0);
@@ -2788,13 +2848,20 @@ pub const Link = struct {
_ = std.fmt.bufPrint(why, "{s}\x00", .{upgrade_words}) catch {};
return null;
}
- if (tries > 200) return null;
+ if (!patient or tries > 200) return null;
const ts: libc.timespec = .{ .sec = 0, .nsec = 100 * std.time.ns_per_ms };
_ = libc.nanosleep(&ts, null);
}
}
};
+test "a client joining again after a divergence is told at once that its session is gone" {
+ var why: [600]u8 = undefined;
+ const t0 = nowNs();
+ try std.testing.expect(Link.redial(std.testing.allocator, "/nonexistent/pardes-replica.sock", "gone", &why, null, false) == null);
+ try std.testing.expect(nowNs() - t0 < 100 * std.time.ns_per_ms);
+}
+
fn appendTo(path: [:0]const u8, line: []const u8) void {
const fd = libc.open(path, .{ .ACCMODE = .WRONLY, .CREAT = true, .APPEND = true, .CLOEXEC = true }, @as(libc.mode_t, 0o644));
if (fd < 0) return;
@@ -3007,6 +3074,53 @@ test "a delta applied under a speculation outlives its rollback: the terminal st
for (rows_a[rows_a.len - shown ..], rows_r[rows_r.len - shown ..]) |x, y| try std.testing.expectEqualStrings(x, y);
}
+test "a view that sized a terminal closing leaves nothing queued: the others' replicas copy, type and agree" {
+ if (comptime !terminal.enabled) return error.SkipZigTest;
+ const gpa = std.testing.allocator;
+ const a = try Authority.init(gpa, .{ .cols = 100, .rows = 30, .boot = .tty }, TestHost.host(), true);
+ defer a.deinit();
+ const first = try gpa.dupe(u8, try a.unit(0, 0));
+ defer gpa.free(first);
+ const r1 = try Replica.init(gpa, 1, first, true);
+ defer r1.deinit();
+ const r2 = try Replica.init(gpa, 2, first, true);
+ defer r2.deinit();
+ a.core.active = for (a.core.panes, 0..) |slot, id| {
+ if (slot) |pane| if (pane.isTerminal()) break id;
+ } else return error.NoShell;
+ const opened = try a.openView(2);
+ r2.view = opened.view;
+ for ([_]*Replica{ r1, r2 }) |r| r.confirm(opened.unit);
+ var sent: std.ArrayListUnmanaged([]u8) = .empty;
+ defer {
+ for (sent.items) |m| gpa.free(m);
+ sent.deinit(gpa);
+ }
+ // The second client, at another size, types into the terminal: its
+ // view sizes it. Then it goes (H10, 2026-10-30: the first aborted).
+ try sent.append(gpa, try gpa.dupe(u8, try r2.input(.{ .resize = .{ .cols = 50, .rows = 20 } })));
+ try key(r2, 'x', &sent);
+ for (sent.items) |m| {
+ const u = try gpa.dupe(u8, try a.take(m));
+ defer gpa.free(u);
+ r1.confirm(u);
+ r1.settle();
+ }
+ r1.confirm(try a.closeView(2));
+ try std.testing.expectEqual(@as(usize, 0), a.core.effects_len);
+ try std.testing.expectEqual(@as(usize, 0), r1.core().effects_len);
+ r1.ready();
+ try std.testing.expect(r1.copy.held);
+ for (sent.items) |m| gpa.free(m);
+ sent.clearRetainingCapacity();
+ try key(r1, 'y', &sent);
+ r1.confirm(try a.take(sent.items[0]));
+ r1.settle();
+ if (r1.diverged) |why| std.debug.print("diverged: {s}\n", .{why});
+ try std.testing.expect(r1.diverged == null and r1.quiet());
+ try std.testing.expectEqual(digestOf(a), r1.digest());
+}
+
/// p50 and p99 of `samples`, sorted in place.
fn spread(samples: []u64) [2]u64 {
std.mem.sort(u64, samples, {}, std.sort.asc(u64));