szn

Bug 479 — isPaneValid regression: O(N·M·P) tree walk per PTY event

479. Three full-tree scans on the server hot loop

Severity: MEDIUM (performance) Status: ✅ FIXED Files: src/server/server.zig:705-728

Problem

Pane.valid was introduced under #305 to make isPaneValid an O(1) flag check. The current code has regressed to a pointer-identity tree walk:

fn isPaneValid(self: *Server, pane: *Pane) bool {
    for (self.sessions.items) |session| {
        for (session.windows.items) |win| {
            for (win.panes.items) |p| {
                if (p == pane) return p.valid;
            }
        }
    }
    return false;
}

(server.zig:719-728) Called from handlePtyEvent for every PTY POLL.IN event — the hottest event in the server.

On top of that, two more functions do the same sessions × windows × panes walk on every loop iteration (run(100) → up to ~10 Hz) even when nothing is pending:

With 3 sessions × 4 windows × 2 panes that is 24 × 3 = 72 tree-iteration passes per tick, plus N×2 pointer comparisons per pty event.

Proposed Fix

Do not simply restore return pane.valid. The tree walk is not an accidental regression: it is the #362 (HIGH, use-after-free) fix. Stale poll events from a batch that killed a session still carry pointers into the freed session arena, so reading pane.valid through the candidate pointer is a UAF (#362, #449). #404 filed this same “regression” and was resolved with a fast O(1) udata guard; liveness must stay safe against freed or address-aliased panes.

Resolution

Tests: loop udata refcount semantics (same-fd re-add, udata replacement, shared udata), pty queue callback, and the sixel list (hook wiring, idempotence, resolve, teardown cleanup).

Verified: zig build test -Doptimize=ReleaseFast — 1012/1012 tests pass.