summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-10-30 21:04:02 -0300
committerGabriel Schneider <[email protected]>2026-10-30 21:28:49 -0300
commite6a114eed043dd1ca50be1cab912662fa9150fda (patch)
tree9a141f1f652807031f98f5f7a943cc0003db812b /src
parent4de480393606fa1f680a4fe84a2507679016efc1 (diff)
downloadpardes-e6a114eed043dd1ca50be1cab912662fa9150fda.tar.gz
pardes-e6a114eed043dd1ca50be1cab912662fa9150fda.zip
dump: a closed terminal brought back on a screen-state replica gets its emulator on the C heap, as terminal.create makes one (terminal.emulatorGpa): loadPane named no `.terminal` allocator, so its emulator was the rollback epoch's, and its screen replica's blobs, made with State.gpa, were freed with the C heap's behind the epoch at a client's end and again by it (H21: `double free or corruption` in Epoch.deinit at a hangup, `free(): invalid pointer` at a rejoin), and a rollback freed what a delta under a held copy made of them
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Diffstat (limited to 'src')
-rw-r--r--src/dump.zig2
-rw-r--r--src/replica.zig25
-rw-r--r--src/terminal.zig10
3 files changed, 34 insertions, 3 deletions
diff --git a/src/dump.zig b/src/dump.zig
index c64f83c4..5f5f5870 100644
--- a/src/dump.zig
+++ b/src/dump.zig
@@ -539,7 +539,7 @@ pub fn savePane(gpa: std.mem.Allocator, pane: *pardes.panes.Pane, slim: bool) ![
pub fn loadPane(p: *Pardes, id: usize, bytes: []const u8) !*pardes.panes.Pane {
var l = try walk.Load(&.{pardes.panes.Pane}).init(p.gpa, bytes);
defer l.deinit();
- l.allocs = .{ .image = p.image_gpa, .pdf = p.pdf_gpa };
+ l.allocs = .{ .image = p.image_gpa, .pdf = p.pdf_gpa, .terminal = pardes.panes.terminal.emulatorGpa(p.gpa) };
const pane = try p.gpa.create(pardes.panes.Pane);
errdefer {
l.rollback();
diff --git a/src/replica.zig b/src/replica.zig
index 439f71a2..da0c3e64 100644
--- a/src/replica.zig
+++ b/src/replica.zig
@@ -3266,6 +3266,31 @@ test "a delta applied under a speculation outlives its rollback: the terminal st
for (rows_a[rows_a.len - shown ..], rows_r[rows_r.len - shown ..]) |x, y| try std.testing.expectEqualStrings(x, y);
}
+test "a closed terminal brought back on a screen-state replica: its emulator on the C heap, never the rollback epoch's" {
+ if (comptime !terminal.enabled) return error.SkipZigTest;
+ const gpa = std.testing.allocator;
+ const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .boot = .tty }, TestHost.host(), true);
+ defer a.deinit();
+ const r = try Replica.init(gpa, 1, try a.unit(0, 0), true);
+ defer r.deinit();
+ const shell: u8 = for (a.core.panes, 0..) |slot, id| {
+ if (slot) |pane| if (pane.isTerminal()) break @intCast(id);
+ } else return error.NoShell;
+ a.core.active = shell;
+ r.confirm(try a.local(.{ .command = "Del" }));
+ try std.testing.expectEqual(@as(usize, 1), a.core.store.closed.items.len);
+ var line: [32]u8 = undefined;
+ r.confirm(try a.local(.{ .command = try std.fmt.bufPrint(&line, "Look @p{d}", .{a.core.store.closed.items[0].serial}) }));
+ try std.testing.expect(r.diverged == null);
+ const back = for (r.core().panes) |slot| {
+ if (slot) |pane| if (pane.terminal) |state| break state;
+ } else return error.NotBack;
+ // Its deltas write it while a copy is held, and its screen replica's
+ // blobs are freed with the C heap's (`deinit`): the epoch's, a rollback
+ // freed what they held and the exit freed them behind its back (H21).
+ try std.testing.expect(r.epoch.owns(@intFromPtr(back)) == null);
+}
+
test "a view that sized a terminal closing leaves nothing queued: the others' replicas copy, type and agree" {
if (comptime !terminal.enabled) return error.SkipZigTest;
const gpa = std.testing.allocator;
diff --git a/src/terminal.zig b/src/terminal.zig
index d4238c83..86eec32d 100644
--- a/src/terminal.zig
+++ b/src/terminal.zig
@@ -607,6 +607,13 @@ pub fn takeMarks(state: *State, bytes: []const u8) void {
/// A screen-state replica's emulators' allocator (`State.gpa`).
pub const emulator_gpa = std.heap.c_allocator;
+/// What a terminal's emulator is made with, by `create` or a pane's load
+/// (dump.loadPane, a closed terminal brought back): `gpa`, but on a screen-
+/// state replica `emulator_gpa`. A hosted build's: the web has no C heap.
+pub fn emulatorGpa(gpa: std.mem.Allocator) std.mem.Allocator {
+ return if (comptime pardes.hosted) (if (pardes.tape.screenReplica()) emulator_gpa else gpa) else gpa;
+}
+
/// 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(comptime why: []const u8) bool {
@@ -936,8 +943,7 @@ 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;
- // A screen-state replica is a hosted build's: the web has no C heap.
- const own = if (comptime pardes.hosted) (if (pardes.tape.screenReplica()) emulator_gpa else gpa) else gpa;
+ const own = emulatorGpa(gpa);
const state = try own.create(State);
errdefer own.destroy(state);
state.gpa = own;