diff options
Diffstat (limited to 'core/replica.zig')
| -rw-r--r-- | core/replica.zig | 44 |
1 files changed, 34 insertions, 10 deletions
diff --git a/core/replica.zig b/core/replica.zig index 781bd211..49787c76 100644 --- a/core/replica.zig +++ b/core/replica.zig @@ -182,6 +182,7 @@ pub const Authority = struct { a.t.whole_digest = screens and std.c.getenv("PARDES_REPLICA_TRACE") == null; // A digest at a unit's end, as the session's (Peers.unit). a.t.no_digest = true; + opts.tape = &a.t; const was = Stepping.begin(&a.t, null); defer was.end(); a.core = try Pardes.init(gpa, opts); @@ -1672,6 +1673,8 @@ pub const Peer = struct { /// Its bye said it joins again for its view (a divergence's rejoin): /// kept for it (views.away), not closed. Its reader's, read after its end. again: bool = false, + /// ...or that its window was closed (Peers.closed). + close: bool = false, /// Its number in the session's `Screens`. sid: u16 = 0, /// Inputs read (each one's seq, as the client numbered them). @@ -1931,7 +1934,9 @@ pub const Peer = struct { switch (t) { .hello => return false, .bye => { - peer.again = (wire.decodeClient(tag, payload) catch return false).bye.again; + const b = (wire.decodeClient(tag, payload) catch return false).bye; + peer.again = b.again; + peer.close = b.close; return false; }, .ping => { @@ -2054,6 +2059,10 @@ pub const Peers = struct { /// No frontend of its own (the host says): with no client either, the /// session is unattended. detached: bool = false, + /// Its last client left by closing its window, and no Detach was typed + /// in any (`kept`): a pair's session ends there (runtime/session.zig). + closed: bool = false, + kept: bool = false, /// The worst a join cost the session's step, and the last one's dump. join_ns: u64 = 0, each_join: std.ArrayListUnmanaged(u64) = .empty, @@ -2353,6 +2362,7 @@ pub const Peers = struct { core.undrawn &= ~(@as(u8, 1) << @intCast(peer.view)); ps.closeViewOf(core, peer, peer.again); if (ps.count() == 0) core.unattended = ps.detached; + if (ps.count() == 0 and peer.close and !ps.kept) ps.closed = true; ps.seq += 1; ps.unit(0, ps.seq); ps.letGo(peer); @@ -2457,6 +2467,7 @@ pub const Peers = struct { /// (`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) { + ps.kept = true; 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; @@ -2927,10 +2938,11 @@ pub fn askCheck(fd: c_int) bool { return writeAll(fd, wire.encodeClient(&out, .check) catch unreachable); } -/// The client's last word: `again`, it joins again for its view at once. -pub fn bye(fd: c_int, again: bool) void { +/// The client's last word: `again`, it joins again for its view at once; +/// `close`, its window was closed. +pub fn bye(fd: c_int, how: @FieldType(wire.ClientMsg, "bye")) void { var out: [16]u8 = undefined; - if (wire.encodeClient(&out, .{ .bye = .{ .again = again } })) |b| _ = writeAll(fd, b) else |_| {} + if (wire.encodeClient(&out, .{ .bye = how })) |b| _ = writeAll(fd, b) else |_| {} } /// What the session streams a client after its dump, as its frontend's loop @@ -2984,6 +2996,13 @@ pub fn ended(why: []const u8) bool { /// The words a client goes with when its session runs another build now. const upgrade_words = "session upgraded to a newer pardes: upgrade this client too"; +/// A join's failure that is its session's running another build now: a +/// client whose own start path holds a newer one execs that instead +/// (runtime/client.zig). +pub fn otherBuild(why: []const u8) bool { + return std.mem.endsWith(u8, std.mem.sliceTo(why, 0), upgrade_words); +} + /// A replica client's join, the terminal's and the window's alike (their /// `runClient`): the session at `path` dialled (or, first, the one on its /// pair, `o.pair`), its build checked to be this @@ -3008,7 +3027,10 @@ pub fn join(gpa: Allocator, j: *Joined, path: []const u8, session: []const u8, o }; if (have) |h| h.left(); prev.deinit(); - } else j.* = sameBuild(gpa, (if (o.pair) |fd| joinOn(gpa, fd, null, why, have, null) else dial(gpa, path, session, why, have, null)) orelse return null, why) orelse return null; + } else j.* = (if (o.join_view) |v| + redial(gpa, path, session, why, have, true, v) + else + sameBuild(gpa, (if (o.pair) |fd| joinOn(gpa, fd, null, why, have, null) else dial(gpa, path, session, why, have, null)) orelse return null, why)) orelse return null; defer { if (have) |h| h.store(gpa, j) else gpa.free(j.dump); j.dump = &.{}; @@ -3043,7 +3065,7 @@ pub fn join(gpa: Allocator, j: *Joined, path: []const u8, session: []const u8, o /// an upgrade under way may not have said yet. fn sameBuild(gpa: Allocator, j: Joined, why: []u8) ?Joined { if (j.semantics == wire.semantics) return j; - bye(j.fd, false); + bye(j.fd, .{}); _ = libc.close(j.fd); var jj = j; jj.rest.deinit(gpa); @@ -3064,7 +3086,10 @@ fn redial(gpa: Allocator, path: []const u8, session: []const u8, why: *[600]u8, while (true) : (tries += 1) { why.* = @splat(0); if (dial(gpa, path, session, why, have, view)) |j| return sameBuild(gpa, j, why); - if (std.mem.indexOf(u8, std.mem.sliceTo(why, 0), "runs another build") != null) { + // Another build, or another protocol (the daemon moved with its + // session): no dial again will do. + const words = std.mem.sliceTo(why, 0); + if (std.mem.indexOf(u8, words, "runs another build") != null or std.mem.indexOf(u8, words, "run the same pardes at both ends") != null) { _ = std.fmt.bufPrint(why, "{s}\x00", .{upgrade_words}) catch {}; return null; } @@ -3081,6 +3106,8 @@ pub const Link = struct { r: *Replica, fd: c_int, quit: bool = false, + /// ...because its window was closed: said in its bye. + close: bool = false, /// The session hung up. gone: bool = false, /// Something to draw: an input applied, a unit taken. @@ -6772,9 +6799,6 @@ test "a client joined from a dump lands a click on a rail's tick as the session r.confirm(try a.take(m)); r.settle(); } - // (The key may be mispredicted: a step's places are a frame's time's - // worth, `tape.over`, the session's verdict, and its drawn caches let - // it find more in that time than the client's speculation did.) try agrees(a, r); try std.testing.expectEqual(a.core.panes[owner].?.places.current, r.core().panes[owner].?.places.current); } |
