summaryrefslogtreecommitdiff
path: root/core/replica.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-26 09:16:28 -0300
committerGabriel Schneider <[email protected]>2026-11-26 09:33:45 -0300
commitaa3ff349f840f568ea0f052c1f051cdc0dbb18c1 (patch)
tree4e47531e47e8ac5ab53af2c75c099fdd88190a93 /core/replica.zig
parent8959b031dc330b4da21c0e89e0831d6fadbcd757 (diff)
downloadpardes-aa3ff349f840f568ea0f052c1f051cdc0dbb18c1.tar.gz
pardes-aa3ff349f840f568ea0f052c1f051cdc0dbb18c1.zip
Kind, dump, replica: a plain kind's state opened or closed in a pane that stays (a picker's preview of a PDF, Pick.showKind; a file changed under its pane; an addon reloaded) is a write outside a client's rollback copy, so a client lets the copy go with that unit and never replays it over the copy (agent T23's find, which blocked the install: the soak `replicas -- --clients 5 --rtt 30,80,207` died with `allocator leaks detected` at seed 0 rtt 80 on T1's base and on M2, MuPDF's allocations of documents a picker previewed: contexts, documents, lanes, from Kind.reopen under Pick.showKind); THE CAUSE: no path of the preview forgets its close (the next row and a source change close through Pick.preview, Esc and every roll-up through restore and putBack, ⏎ through land's roll-up, a pane's end through teardownPane, the session's through Core.deinit; a dump carries the slot and the client's join opens it, closed the same ways), but replica.confirm's `ahead` rule: a client idle with its copy taken ahead (Replica.ready) replays the session's units on it and keeps each in `since` to replay again when the copy is put back, unless the unit wrote outside the copy (Pardes.outside, counted by the emulators alone) or opened or closed a pane (its serials); a preview opens the pdf addon's state in the pane the picker lands in, which stays, and that state (the document and its lanes, MuPDF's on the kind heap, the addon's `Own` found by its block's address) is no copy's; so a rollback (another's unit before the client's own key) put the copy back, the epoch freed the block, and the replay opened the document again for a new block: the first was never closed (and its `Own`, keyed by an address the epoch hands out again, could be found for another block); THE FIX: Kind.reopen and Kind.close count a plain kind's open and close in Pardes.outside, by the registry's PA_PLAIN, on every side alike whether the kind's code runs there or not (a client without the library counts as the session does), so confirm's one question lets such a unit go with the copy; dump.plainHere, a client's own open of a plain kind's pane at its join and no step of the session's, puts the count back to the session's as dumped; Term.Outside's and Pardes.outside's docs say so; RUN (TEST FIRST): replica.zig "a picker's preview opens and closes a plain kind's state in the pane it lands in, no pane opened or closed: ..." (a plain test kind `Owned` noting each state it opens and letting it go at its close, as the pdf addon's `Own`; files `.own`): a session at notes.txt in a work tree with paper.own, a client joined in its own view running the kind, idle with its copy taken ahead while the session types `SPC f f p` (the preview through the kind in notes.txt's pane), then a key of the client's own on top and the session's own key first (a rollback), the client's key, Esc; asks the digests equal at each end and no state left noted after both cores end: fails on M2 (8959b031dc33) at the last check (expected 0, found 1), passes now; the soak with the pdf addon (`zig build replicas -Dplatform=tui -- --clients 5 --rtt 30,80,207`, every addon the build has): diverged 0, unequal 0, quiet checks 508/0 474/0 665/0, stops not on may_wait 0, the corpus 22 of 22 scenes the same, no leak, the pdf panes the session opened 8, 6 and 5; a user could hit it as a client of a session whose picker previewed a PDF (a far client in a pair, a window joined to a terminal's session): a document leaked in the client at each rollback over such a preview, and pages drawn from another document's state where an address came back; NEW HOT PATHS: none per key or frame (a plain kind's open and close count one integer; a client lets its copy go at such a unit, as at a pane's open); GATES: core-test 1163/1337 (174 skipped: +1), its globals test (`nothing of a core is a process global`: the test kind's var in its test); no dump layout change (Pardes.outside was dumped and digested already; its values move at a plain kind's open and close, on every side alike); load 1.0 to 8.4
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNBFhdevVu48bvL9kCE
Diffstat (limited to 'core/replica.zig')
-rw-r--r--core/replica.zig112
1 files changed, 112 insertions, 0 deletions
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());
+}