Severity: MEDIUM (performance)
Status: ✅ FIXED
Files: src/server/server.zig:705-728
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:
pumpPaneInput (server.zig:705-717) — walks the whole tree just to find panes with non-empty pty.input_buf.tickSixelWait (server.zig:2909-2940) — walks the whole tree looking for panes with pending_sixel.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.
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.
isPaneValid: make the liveness check O(1) without dereferencing the
candidate pointer — e.g. a per-pane generation id validated against a small
liveness epoch, or explicit udata deregistration in every kill path
(destroyPane, killSession, killAllSessions, killWindow). The #362/#449
regression tests must stay green.pumpPaneInput: maintain a set/list of panes with queued input (pty.input_buf non-empty) and iterate only that.tickSixelWait: maintain a set of panes with pending_sixel and iterate only that.isPaneValid (src/server/server.zig): fast path checks
Loop.isUdataRegistered(@intFromPtr(pane)) — a new refcounted udata set in
src/server/loop.zig maintained by addFd/removeFd — and only then reads
pane.valid. A miss falls back to the identity-only tree walk, preserving
the old semantics for deliberately unwatched panes (pty closed after
remain-on-exit) without ever dereferencing freed memory. The #362/#449
regression tests were updated to register a mock pty and stay green.pumpPaneInput deleted: Pty.writeInput now fires an input_queued_cb
hook when bytes must be queued, and Server.onPtyInputQueued arms POLLOUT
immediately — the per-tick tree scan is gone. watchPanePty also re-arms
POLLOUT when a re-watched pty still has queued input (addFd replaces the
entry’s event bits).tickSixelWait iterates Server.sixel_wait_panes, populated by a
Pane.feedPty hook (Pane.sixel_pending_hook) after a feed leaves a sixel
pending. Entries are dropped when the sixel resolves and scrubbed in
destroyPane, killSession, killAllSessions and unregisterWindowFds
before the pane memory is freed.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.