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 /src/terminal.zig | |
| 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]>
Diffstat (limited to 'src/terminal.zig')
| -rw-r--r-- | src/terminal.zig | 28 |
1 files changed, 20 insertions, 8 deletions
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; |
