szn

Bug 528 — splitPane OOM desyncs the tree and the pane list

528. Tree mutation precedes the panes.append, with no rollback

Severity: MEDIUM Status: ✅ FIXED Files: src/window.zig:454, src/window.zig:458

Verification status: identified by static analysis; no failing-allocator repro was built.

Problem

// src/window.zig:454
const new_pane = try self.layout.splitPane(self.allocator, pane, dir, proportion);
...
// src/window.zig:458
try self.panes.append(self.allocator, new_pane);

By the time panes.append runs, layout.splitPane has already inserted new_pane into the tree and resized the parent pane. If the append fails, the new pane is live in the tree — it will be rendered, walked by collectPaneBounds, and rotated by rotatePanes — but is absent from window.panes, which breaks pane indexing and the “window.panes owns the panes” lifetime invariant.

This is the ordering inverse of the fix pattern used elsewhere in the tree: mutate the container before the structure, or wrap the post-split steps in an errdefer that calls layout.removePane(new_pane) and restores the parent size.

Proposed Fix

Append to self.panes before mutating the tree, or add an errdefer rollback after the split:

const new_pane = try self.layout.splitPane(self.allocator, pane, dir, proportion);
errdefer self.layout.removePane(new_pane);
try self.panes.append(self.allocator, new_pane);

Note that errdefer ordering matters here — see bug #426, which was an errdefer-ordering defect in a different path.

Fix

// src/window.zig — Window.splitPane
const old_w = pane.screen.grid.width;
const old_h = pane.screen.grid.height;
const new_pane = try self.layout.splitPane(self.allocator, pane, dir, proportion);
errdefer {
    self.layout.removePane(new_pane);
    pane.resizeTerminal(old_w, old_h) catch |err|
        std.log.warn("resizeTerminal failed: {any}", .{err});
    new_pane.deinit();
    self.allocator.destroy(new_pane);
}

The rollback runs for any failure after the split — the panes.append step and the setHistoryLimit call before it. It undoes all three effects of a split: the tree gains the pane, pane is shrunk, and the new pane is allocated.

Layout.removePane collapses the split and leaves the pane orphaned rather than destroying it, so the errdefer frees it explicitly, mirroring what Layout.deinitNode does for a leaf.

Regression test

splitPane rolls the layout tree back when the pane append fails — bug #528 (src/window.zig) sweeps FailingAllocator.fail_index 0..11. The FailingAllocator is injected into the Window (not the Layout) so the failure lands on panes.append — the step that used to desync the two. On the failure path it asserts window.panes.len and layout.countLeaves() are both unchanged, and on every path that the two agree.

Verified with the full suite on Zig 0.16.0 and 0.17.0: 1055/1055 passing on both.

Bugs #232 (window/layout tree desync on last-pane removal and rotation), #254 (parent pane dimensions un-restored on split allocation failure), #318 (layout split failure leaving pane dimensions un-restored) and #398 (Window.addPane / init double-destroying the pane when Pane.init fails). This is the remaining member of that family: the append failure after a successful split.