szn

Bug 56 — destroyPane iterates self.sessions while killSession swapRemoves from it

56. destroyPane iterates self.sessions while killSession swapRemoves from it

File: src/server/server.zig:261–293 Severity: MEDIUM Status: ✅ FIXED — refactored destroyPane to a scan-then-destroy pattern, avoiding loop mutation bugs.

pub fn destroyPane(self: *Server, pane: *Pane) void {
    for (self.sessions.items) |session| {          // outer loop
        for (session.windows.items) |win| {         // middle loop
            for (win.panes.items) |p| {              // inner loop
                if (p == pane) {
                    win.removePane(self.allocator, pane);
                    if (win.panes.items.len == 0) {
                        session.killWindow(self.allocator, win);
                    }
                    if (session.windows.items.len == 0) {
                        self.killSession(session.name) catch {};
                        // ^^ swapRemove on self.sessions
                    }
                    ...
                    return;  // early exit salvages correctness
                }
            }
        }
    }
}

Currently safe because the return at line 288 exits all loops immediately after a mutation. However, the three loops are simultaneously invalidated:

If anyone adds code after the return statement (e.g. a break to the outer loop, or additional cleanup), the iterator will access stale/moved elements. Should be refactored to a single find then destroy pattern.