diff options
| author | Gabriel Schneider <[email protected]> | 2026-10-29 22:01:29 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-10-29 22:30:29 -0300 |
| commit | 77c4a6e346e08538abc410f85a86bec7e8dbedb3 (patch) | |
| tree | 02deb84c5276c9362c7e6929bfc4f23aaeb3b48d | |
| parent | 473bbaa838f290e932992ef625be2fdc05c952dc (diff) | |
| download | pardes-77c4a6e346e08538abc410f85a86bec7e8dbedb3.tar.gz pardes-77c4a6e346e08538abc410f85a86bec7e8dbedb3.zip | |
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 <[email protected]>
| -rw-r--r-- | src/replica.zig | 33 | ||||
| -rw-r--r-- | src/rollback.zig | 12 | ||||
| -rw-r--r-- | src/terminal.zig | 28 |
3 files changed, 36 insertions, 37 deletions
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; }, diff --git a/src/rollback.zig b/src/rollback.zig index d87671e6..79b8202e 100644 --- a/src/rollback.zig +++ b/src/rollback.zig @@ -140,18 +140,6 @@ pub const Epoch = struct { _ = e.live.remove(addr); } - /// What is allocated and freed until `unpause` is no copy's: a delta - /// written into a replica's terminal while a copy is held (an emulator - /// is the authority's, outside every copy, and must outlive a rollback). - pub fn pause(e: *Epoch) bool { - defer e.on = false; - return e.on; - } - - pub fn unpause(e: *Epoch, was: bool) void { - e.on = was; - } - fn begin(e: *Epoch) void { std.debug.assert(!e.on); e.id +%= 1; diff --git a/src/terminal.zig b/src/terminal.zig index fcb284a0..aedc007f 100644 --- a/src/terminal.zig +++ b/src/terminal.zig @@ -143,6 +143,11 @@ pub const MarkedHandler = if (enabled) struct { } else void; pub const State = struct { + /// What the emulator and this struct are allocated with: the core's, + /// but on a screen-state replica the C heap (`emulator_gpa`), outside + /// its rollback epoch: written by deltas while a copy is held, no part + /// of it is ever a copy's to save or put back. + gpa: std.mem.Allocator, vt: VtSlot, stream: StreamSlot, /// The emulator's answers to its program (DSR, DA, kitty graphics, ...) @@ -281,7 +286,7 @@ pub const State = struct { .kitty_image_storage_limit = if (images_enabled) TerminalImages.storage_limit else 0, }); defer decoded.deinit(gpa); - out.* = .{ .vt = decoded.toOwned(), .stream = undefined }; + out.* = .{ .gpa = gpa, .vt = decoded.toOwned(), .stream = undefined }; if (comptime images_enabled) inline for (.{ .primary, .alternate }) |key| { if (out.vt.screens.get(key)) |screen| screen.kitty_images.image_limits = .allWithTempDir(tempDir()); }; @@ -582,6 +587,9 @@ pub fn takeMarks(state: *State, bytes: []const u8) void { m.finished = if (seq == std.math.maxInt(u32)) null else .{ .seq = seq, .status = if (status == std.math.minInt(i32)) null else status }; } +/// A screen-state replica's emulators' allocator (`State.gpa`). +pub const emulator_gpa = std.heap.c_allocator; + /// A replica's speculation leaves an emulator be: it is the authority's /// (docs/next-daemon.md, phase 5), so the input waits for its order. fn authorityOnly() bool { @@ -629,7 +637,8 @@ pub fn resizeGrid(pane: *Pane, gpa: std.mem.Allocator, cols: u16, rows: u16) voi // A replica's emulator is sized by its authority: the next delta says. if (pardes.tape.screenReplica()) return; state.changes +%= 1; - state.vt.resize(gpa, .{ .cols = cols, .rows = rows }) catch {}; + _ = gpa; + state.vt.resize(state.gpa, .{ .cols = cols, .rows = rows }) catch {}; } /// DECSET 2004: the program wants its pastes bracketed. @@ -829,8 +838,9 @@ pub fn deinitEmulator(pane: *Pane, gpa: std.mem.Allocator) void { const state = pane.terminal orelse return; if (comptime images_enabled) state.images.deinit(gpa); state.stream.deinit(); - state.vt.deinit(gpa); - gpa.destroy(state); + const own = state.gpa; + state.vt.deinit(own); + own.destroy(state); pane.terminal = null; } @@ -841,9 +851,11 @@ pub fn create(gpa: std.mem.Allocator, cols: u16, rows: u16) !*Pane { const pane = try createDoc(gpa, cols, rows); errdefer gpa.destroy(pane); if (comptime !enabled) return pane; - const state = try gpa.create(State); - errdefer gpa.destroy(state); - state.vt = try ghostty_vt.Terminal.init(terminalIo(), gpa, .{ + const own = if (pardes.tape.screenReplica()) emulator_gpa else gpa; + const state = try own.create(State); + errdefer own.destroy(state); + state.gpa = own; + state.vt = try ghostty_vt.Terminal.init(terminalIo(), own, .{ .cols = cols, .rows = rows, .max_scrollback_bytes = 16 * 1024 * 1024, @@ -856,7 +868,7 @@ pub fn create(gpa: std.mem.Allocator, cols: u16, rows: u16) !*Pane { // it could send itself: it runs as the user, under pardes. .kitty_image_loading_limits = if (images_enabled) .allWithTempDir(tempDir()) else if (@FieldType(ghostty_vt.Terminal.Options, "kitty_image_loading_limits") == void) {} else .direct, }); - state.stream = .init(.{ .handler = .{ .inner = state.vt.vtHandler() }, .allocator = gpa }); + state.stream = .init(.{ .handler = .{ .inner = state.vt.vtHandler() }, .allocator = own }); state.reply_len = 0; state.changes = 0; state.read = false; |
