From 77c4a6e346e08538abc410f85a86bec7e8dbedb3 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Thu, 29 Oct 2026 22:01:29 -0300 Subject: screen-state replicas: a replica's emulators on the C heap, outside its rollback epoch, so a delta written under a held copy is no copy's to keep or put back (a ReleaseFast run found the heap corrupted) terminal.zig: State.gpa, what an emulator and its State are allocated with: the core's, but on a screen-state replica the C heap (emulator_gpa). replica.zig applies deltas and rows with it; the epoch's pause is gone (rollback.zig back to part A's but for the rows cache). The harness in ReleaseFast corrupted the heap after 20 seeds: a safe build's check named an allocation the copy saved and a delta freed while the epoch was paused, reached as `Text.own` through a view's stale slot (views.zig keeps the live view's parked share as stale copies, and the copy walks it), the replica's old terminal's pin map at that address. With the emulator outside the epoch, nothing of it is saved, kept or put back, and every free under a held copy is put off again, as in part A. Debug, 20 seeds at 80 ms and the defaults: no divergence, every replica and terminal the authority's. Co-Authored-By: Claude Opus 5.5 --- src/replica.zig | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) (limited to 'src/replica.zig') diff --git a/src/replica.zig b/src/replica.zig index cac8ed59..a96a056a 100644 --- a/src/replica.zig +++ b/src/replica.zig @@ -222,14 +222,18 @@ pub const Authority = struct { // // A message of either side is u8 pane, u32 the pane's serial (a slot's // next pane is another), u8 kind, and the kind's bytes: -// to a replica: kind 1, a delta; kind 2, rows it asked for (encodeRows); +// to a replica: kind 1, a delta; kind 2, rows (encodeRows): asked for, +// or a step read them; // to the authority: kind 1, an acknowledgement (u32 seq); kind 2, a // request for rows (u64 lo, u64 hi, absolute); kind 3, a // delta refused: start this terminal afresh. -// The authority sends a peer what it is owed (`Screens.flush`) before it -// steps any input but a pty's output, and once a frame; a replica takes -// them in that order between units (`Replica.screen`). So a step a replica -// replays reads the terminal its authority's read. +// In the authority's stream to a peer, before it steps any input but a +// pty's output, and once a frame: its own steps since the last unit, as one +// (`Authority.pending`), then what the peer is owed of the terminals +// (`Screens.flush`). After a step, before its unit: the rows it read that +// the peer holds as holes (`Screens.fill`, then `filled`). A replica takes +// them in that order between units (`Replica.screen`), so a step it replays +// reads the terminal its authority's step read. const kind_delta = 1; const kind_rows = 2; @@ -490,7 +494,7 @@ pub const Replica = struct { } pub fn deinit(r: *Replica) void { - for (&r.screens) |*slot| if (slot.*) |*sc| sc.rep.deinit(r.coreGpa()); + for (&r.screens) |*slot| if (slot.*) |*sc| sc.rep.deinit(terminal.emulator_gpa); if (r.t.core) |c| { const was = Stepping.begin(&r.t, &r.epoch); if (r.copy.held) r.copy.restore(); @@ -718,10 +722,6 @@ pub const Replica = struct { return r.pending.items.len == 0 and r.applied == 0 and !r.copy.held; } - fn coreGpa(r: *Replica) Allocator { - return r.epoch.allocator(); - } - /// One of the authority's terminal messages (`Screens.flush`, or an /// answer to `screenRequests`), taken between units in stream order: a /// delta written into its pane's emulator, or fetched rows. The @@ -735,18 +735,17 @@ pub const Replica = struct { const state = pane.terminal orelse return null; const slot = &r.screens[m.pane]; if (slot.* == null or slot.*.?.serial != m.serial) { - if (slot.*) |*old| old.rep.deinit(r.coreGpa()); + if (slot.*) |*old| old.rep.deinit(terminal.emulator_gpa); slot.* = .{ .serial = m.serial }; } const sc = &slot.*.?; - // The emulator is the authority's, outside every rollback copy: - // what it allocates now must outlive one. - const was = r.epoch.pause(); - defer r.epoch.unpause(was); + // The emulator is the authority's: on the C heap, outside the + // rollback epoch, so a copy held now never saves or puts back any + // of what this writes (State.gpa). switch (m.kind) { kind_delta => { const t0 = nowNs(); - const applied = sc.rep.apply(r.coreGpa(), terminal.terminalIo(), &state.vt, m.payload) catch |err| { + const applied = sc.rep.apply(state.gpa, terminal.terminalIo(), &state.vt, m.payload) catch |err| { if (err != error.BaseMismatch) r.fail("a terminal's delta did not apply"); if (sc.refused) return null; sc.refused = true; @@ -763,7 +762,7 @@ pub const Replica = struct { }, kind_rows => { sc.asked = null; - sc.rep.applyRows(r.coreGpa(), &state.vt, m.payload) catch r.fail("a terminal's rows did not apply"); + sc.rep.applyRows(state.gpa, &state.vt, m.payload) catch r.fail("a terminal's rows did not apply"); c.shell_rows.markStale(pane); return null; }, -- cgit v1.3