summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/replica.zig67
1 files changed, 66 insertions, 1 deletions
diff --git a/src/replica.zig b/src/replica.zig
index 4140beca..3af1caac 100644
--- a/src/replica.zig
+++ b/src/replica.zig
@@ -741,6 +741,10 @@ pub const Replica = struct {
/// as the last delta since set it: the copy holds no emulator, and a
/// speculation may move a view, so a rollback puts these back.
views: [pardes.MAX_PANES]?usize = @splat(null),
+ /// Its panes' serials as the copy was taken, and whether one was being
+ /// torn down then (`panesMoved`).
+ serials: [pardes.MAX_PANES]u32 = @splat(0),
+ retiring: bool = false,
screen_out: std.ArrayListUnmanaged(u8) = .empty,
/// Its Shadows' rasters, from the session's strips (17.3).
blobs: pardes.Kind.Blobs = .{},
@@ -842,7 +846,13 @@ pub const Replica = struct {
for (&r.screens) |*slot| if (slot.*) |*sc| Term.replicaFree(sc.rep, pardes.panes.emulator_gpa);
if (r.t.core) |c| {
const was = Stepping.begin(&r.t, &r.epoch);
- if (r.copy.held) _ = r.copy.restore();
+ // The state it ends on is whole as it is: the copy let go, its
+ // put-off frees done. Put back, it would bring back what was
+ // freed between steps meanwhile (the shell's rows a draw read
+ // again after a delta), which `toConfirmed` drops and this
+ // freed again (a held copy across an input of its own, a
+ // rejoin's old replica).
+ if (r.copy.held) r.copy.release();
c.deinit();
was.end();
}
@@ -942,6 +952,7 @@ pub const Replica = struct {
r.dirty = true;
return;
}
+ const read_terminal = r.pending.items[0].terminal;
r.freePending(r.pending.orderedRemove(0));
r.applied -= 1;
if (r.applied == 0) {
@@ -957,6 +968,15 @@ pub const Replica = struct {
return r.fail(why);
}
}
+ // The copy stays the point a rollback goes back to, this
+ // input kept to replay over it as a unit of the session's
+ // that comes while idle is: a step writes some bytes of the
+ // megabyte the copy is, walked once in `rebase_after` units,
+ // not once a key. Not over one that read a terminal (replayed
+ // later it would read the deltas since), nor once a pane
+ // opened, closed or was torn down since the copy (what it
+ // holds outside the copy was made or freed: `ahead`'s rule).
+ if (!read_terminal and !r.panesMoved()) return r.keepSince(u.tape);
r.copy.release();
rollback.Epoch.select(null);
r.clearSince();
@@ -1208,10 +1228,25 @@ pub const Replica = struct {
}
fn keepViews(r: *Replica) void {
+ const c = r.core();
+ for (c.panes, &r.serials) |slot, *n| n.* = if (slot) |pane| pane.serial else 0;
+ r.retiring = for (c.retired_panes.current, c.retired_panes.previous) |x, y| {
+ if (x != null or y != null) break true;
+ } else false;
if (!r.t.screen_state) return;
for (r.core().panes, &r.views) |slot, *v| v.* = if (slot) |pane| if (pane.terminal) |state| pardes.Term.grid(state).view else null else null;
}
+ /// Whether a pane opened, closed or is torn down since the copy was
+ /// taken (`keepViews`).
+ fn panesMoved(r: *const Replica) bool {
+ const c = r.core();
+ if (r.retiring) return true;
+ for (c.retired_panes.current, c.retired_panes.previous) |x, y| if (x != null or y != null) return true;
+ for (c.panes, r.serials) |slot, n| if ((if (slot) |pane| pane.serial else 0) != n) return true;
+ return false;
+ }
+
fn putViews(r: *Replica) void {
if (!r.t.screen_state) return;
for (r.core().panes, r.views) |slot, at| if (slot) |pane| if (pane.terminal) |state| pardes.Term.viewTo(state, at);
@@ -3616,6 +3651,36 @@ test "another client's input first: a replica rolls back, replays it and applies
try std.testing.expectEqual(@as(u32, 4), r2.stats.fast);
}
+test "the copy taken for an input of its own stays once the input is confirmed: the next takes none, and a rollback replays the first over it" {
+ const gpa = std.testing.allocator;
+ const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host(), false);
+ defer a.deinit();
+ const first = try gpa.dupe(u8, try a.unit(0, 0));
+ defer gpa.free(first);
+ const r = try Replica.init(gpa, 1, first, false);
+ defer r.deinit();
+ var sent: std.ArrayListUnmanaged([]u8) = .empty;
+ defer {
+ for (sent.items) |m| gpa.free(m);
+ sent.deinit(gpa);
+ }
+ freshSecond();
+ try key(r, 'j', &sent);
+ const copies = r.stats.copies;
+ r.confirm(try a.take(sent.items[0]));
+ try std.testing.expect(r.copy.held);
+ try std.testing.expectEqual(@as(usize, 1), r.since.items.len);
+ try key(r, 'j', &sent);
+ try std.testing.expectEqual(copies, r.stats.copies);
+ // The session's own key first: back to the copy, the first replayed.
+ r.confirm(try a.local(.{ .key = .{ .cp = 'l', .text = "l" } }));
+ r.settle();
+ r.confirm(try a.take(sent.items[1]));
+ r.settle();
+ try agrees(a, r);
+ try std.testing.expectEqual(@as(u32, 1), r.stats.foreign);
+}
+
test "a rollback over a Tab it applied puts the process's tab width back as its core has it" {
// config.tab_now is the process's, written by the step that set the
// width (the soak, five clients: a Tab undone left it, and the next