summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--core/Core.zig7
-rw-r--r--core/Kind.zig13
-rw-r--r--core/Term.zig8
-rw-r--r--core/dump.zig4
-rw-r--r--core/replica.zig112
5 files changed, 137 insertions, 7 deletions
diff --git a/core/Core.zig b/core/Core.zig
index 3a2e1f51..b80fec2b 100644
--- a/core/Core.zig
+++ b/core/Core.zig
@@ -304,9 +304,10 @@ n_look_src: usize = 0,
look_walk_owner: ?u32 = null,
/// hands out Pane.serial; monotonic, never reused
next_serial: u32 = 0,
-/// The writes of its terminals' emulators, which no rollback copy holds
-/// (Term.Outside): digested, dumped and copied as any field, so the same on
-/// every side; the replica engine's one question of a unit reads it.
+/// The writes of its terminals' emulators, and the opens and closes of its
+/// plain kinds' states (Kind.reopen, Kind.close), which no rollback copy
+/// holds (Term.Outside): digested, dumped and copied as any field, so the
+/// same on every side; the replica engine's one question of a unit reads it.
outside: pardes.Term.Outside = .{},
/// The column whose tag the running command came from, while it runs:
/// the column acme's commands take from their Text's col.
diff --git a/core/Kind.zig b/core/Kind.zig
index 4841a55c..22a7ebe0 100644
--- a/core/Kind.zig
+++ b/core/Kind.zig
@@ -320,10 +320,16 @@ pub fn reopen(p: *Pardes, id: usize, s: *Slot, bytes: ?[]const u8) bool {
if (p.kinds.byName(s.nameOf())) |k| {
// Its code: its library loaded at the first pane of its kind.
if (s.ops == null) s.ops = Addon.need(&p.kinds, p.tape, k, &cl.refused, p.opts.distrusted);
- // Its block made afresh by this open.
+ // Its block made afresh by this open. And what a plain kind keeps
+ // beside it (a PDF's document), made in every core that runs it, is
+ // no rollback copy's: a write outside the copy (Pardes.outside),
+ // counted by the registry's word on every side alike, the kind's
+ // code here or not. A client that kept this step to replay over its
+ // copy would make that state again and never close the first.
if (p.kinds.kinds[k].caps.plain) {
if (s.plain) |b| if (b.len > 0) s.gpa.free(b);
s.plain = &.{};
+ p.outside.n +%= 1;
}
// Its tag's computed words (UX K'3): its words, then Del.
var w: std.Io.Writer = .fixed(&s.tail.bytes);
@@ -416,6 +422,11 @@ pub fn close(p: *Pardes, pane: *Pane) void {
// own memory (its pictures) is left be, as the rollback brings the pane
// back; the session's order closes it.
const kept = s.plain != null and s.ops != null and s.ops.?.close != null and p.tape.authorityOnly("a kind's close");
+ // What a plain kind keeps beside its block let go: outside the copy, as
+ // its open was (reopen).
+ if (p.kinds.byName(s.nameOf())) |k| if (p.kinds.kinds[k].caps.plain) {
+ p.outside.n +%= 1;
+ };
if (!kept) {
var i: usize = 0;
while (i < ready.items.len) if (ready.items[i].draw == p and ready.items[i].serial == pane.serial) ready.orderedRemove(i).drop() else {
diff --git a/core/Term.zig b/core/Term.zig
index 509da6d9..2f0a0f3f 100644
--- a/core/Term.zig
+++ b/core/Term.zig
@@ -19,9 +19,11 @@ const iface = plain.term;
/// digested and dumped, a function of the tape, the same on every side (one
/// side's emulator written where the others' are not is a digest that says
/// `outside`). Every write below takes it and counts, and so does a write a
-/// speculation owes (`resize`): over an input's apply, the count moved is
-/// "this input wrote, or owes, state outside the copy", the one question
-/// core/replica.zig `confirm` asks a unit. A load sets it from the dump.
+/// speculation owes (`resize`), and a plain kind's state opened or closed
+/// (Kind.reopen, Kind.close: what it keeps beside its block, a PDF's
+/// document): over an input's apply, the count moved is "this input wrote,
+/// or owes, state outside the copy", the one question core/replica.zig
+/// `confirm` asks a unit. A load sets it from the dump.
pub const Outside = struct { n: u64 = 0 };
/// The reloads this process made (a join's, on every side) and their time,
/// for the soak's join line (test/replicas.zig `--bytes`).
diff --git a/core/dump.zig b/core/dump.zig
index 0cefa157..389d948f 100644
--- a/core/dump.zig
+++ b/core/dump.zig
@@ -1010,6 +1010,10 @@ pub fn plainHere(p: *Pardes, id: usize, old: *Kind.Slot, ops: *const Kind.Ops) !
s.selection = try p.gpa.dupe(u8, old.selection);
// (and its place: the open's own `where` is of a block not yet the session's)
const kept = .{ old.next_tick, old.views, old.focused, old.state, old.where_text };
+ // The count of writes outside the copy stays the session's, as dumped:
+ // this open is the join's, no step the session took (Kind.reopen).
+ const outside = p.outside;
+ defer p.outside = outside;
if (!Kind.reopen(p, id, s, old.file)) return error.KindRefused;
// Its block the session's, byte for byte (its record), never through
// the kind's load, which may tidy it.
diff --git a/core/replica.zig b/core/replica.zig
index 6c037cd2..9e6183a1 100644
--- a/core/replica.zig
+++ b/core/replica.zig
@@ -9203,3 +9203,115 @@ test "a client joined after its session's shell was found to have no prompt mark
_ = try n.serve(.{ .tag = 4, .op = .release, .node = run, .handle = h, .opened = true }, true);
try n.agree();
}
+
+test "a picker's preview opens and closes a plain kind's state in the pane it lands in, no pane opened or closed: a client with a copy ahead never replays it over the copy, and every state the kind opens is closed once" {
+ // The soak's (test/replicas.zig with the pdf addon): `allocator leaks
+ // detected` at seed 0 rtt 80, the documents of PDFs a picker previewed
+ // (Pick.showKind) on a client, opened by a unit it kept to replay over
+ // its copy and opened again by the replay, the first never closed.
+ // A plain kind that holds something of its own outside its block for
+ // each state it opens, as the pdf addon holds a document (its `Own`):
+ // each open notes its block, each close lets it go, so a state opened
+ // and never closed stays noted. Its files are `.own`.
+ const Owned = struct {
+ const pa = pardes.Kind.c;
+ var noted: [8]?*anyopaque = @splat(null);
+ fn open(_: [*c]const u8, _: [*c]const u8, _: u64) callconv(.c) ?*anyopaque {
+ const st = pardes.Kind.api.alloc.?(4);
+ for (&noted) |*n| if (n.* == null) {
+ n.* = st;
+ break;
+ };
+ return st;
+ }
+ fn close(st: ?*anyopaque) callconv(.c) void {
+ for (&noted) |*n| if (n.* == st) {
+ n.* = null;
+ return;
+ };
+ }
+ fn paint(_: ?*anyopaque, _: [*c]const pa.pa_window, _: [*c]pa.pa_paint) callconv(.c) void {}
+ fn held() usize {
+ var n: usize = 0;
+ for (noted) |st| n += @intFromBool(st != null);
+ return n;
+ }
+ const ops = std.mem.zeroInit(pardes.Kind.Ops, .{
+ .head = std.mem.zeroInit(pa.pa_head, .{ .size = @sizeOf(pa.pa_head), .abi = pardes.Kind.abi, .name = "owned".* ++ @as([11]u8, @splat(0)), .version = 1, .exts = ".own", .flags = pa.PA_PLAIN }),
+ .open = open,
+ .close = close,
+ .paint = paint,
+ });
+ };
+ const gpa = std.testing.allocator;
+ var tmp = std.testing.tmpDir(.{});
+ defer tmp.cleanup();
+ try tmp.dir.createDir(std.testing.io, ".git", .default_dir);
+ try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "notes.txt", .data = "the notes\n" });
+ try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "paper.own", .data = "own" });
+ var dir_buf: [std.Io.Dir.max_path_bytes]u8 = undefined;
+ const dir = dir_buf[0..try tmp.dir.realPath(std.testing.io, &dir_buf)];
+ var line: [std.Io.Dir.max_path_bytes + 32]u8 = undefined;
+ {
+ const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host());
+ defer a.deinit();
+ var words: [64]pardes.Kind.LoadedWord = undefined;
+ const owned = pardes.Kind.loadedOf(&Owned.ops, "/x/owned.so", @splat(7), &words);
+ _ = try a.local(.{ .addon = .{ .op = .load, .pane = .at(0), .kind = &owned } });
+ _ = try a.local(.{ .command = try std.fmt.bufPrint(&line, "Look {s}/notes.txt", .{dir}) });
+ // A client in a view of its own, running the kind's code.
+ const joined = (try a.join(1, null)).?;
+ defer gpa.free(joined.dump);
+ const r = try Replica.initDump(gpa, 1, joined.view, joined.dump, .{ .cols = 80, .rows = 24 });
+ defer r.deinit();
+ r.core().kinds.ops[r.core().kinds.byName("owned").?] = &Owned.ops;
+ // SPC f f p at the session, in its view: its picker lists the tree,
+ // and paper.own, the one row, shows through the kind in the pane it
+ // lands in. The client idle meanwhile: its copy taken ahead, each
+ // unit replayed on top of it.
+ const At = struct {
+ fn sessionView(au: *Authority) void {
+ const was = Stepping.begin(&au.t, null);
+ au.core.toView(.at(0));
+ was.end();
+ }
+ };
+ At.sessionView(a);
+ for (" ffp") |c| {
+ r.ready();
+ r.confirm(try a.local(.{ .key = .{ .cp = c, .text = &.{c} } }));
+ r.ready();
+ r.confirm(try a.local(.tick));
+ }
+ const landing = for (a.core.panes, 0..) |slot, id| {
+ if (slot) |pane| if (pane.addon != null) break id;
+ } else return error.NoPreview;
+ try std.testing.expect(r.core().panes[landing].?.addon != null);
+ // A key of the client's own on top, and the session's own key
+ // first: the client back to its copy, the units since replayed.
+ var sent: std.ArrayListUnmanaged([]u8) = .empty;
+ defer {
+ for (sent.items) |m| gpa.free(m);
+ sent.deinit(gpa);
+ }
+ try key(r, 'j', &sent);
+ try std.testing.expectEqual(@as(usize, 1), r.applied);
+ const foreign = r.stats.foreign;
+ r.confirm(try a.local(.{ .key = .{ .cp = 'q', .text = "q" } }));
+ r.settle();
+ try std.testing.expectEqual(foreign + 1, r.stats.foreign);
+ for (sent.items) |m| {
+ r.confirm(try a.take(m));
+ r.settle();
+ }
+ try agrees(a, r);
+ // Esc in the picker's view: the preview's state closed, the pane's
+ // own text back.
+ At.sessionView(a);
+ r.confirm(try a.local(.{ .key = .{ .cp = pardes.Key.escape } }));
+ r.settle();
+ try agrees(a, r);
+ try std.testing.expect(r.core().panes[landing].?.addon == null);
+ }
+ try std.testing.expectEqual(@as(usize, 0), Owned.held());
+}