szn

Bug 400 — Stale PTY poll event calls removeFd(ev.fd) by fd number and can unregister a recycled fd’s NEW owner (frozen pane)

400. Stale PTY poll event calls removeFd(ev.fd) by fd number and can unregister a recycled fd’s NEW owner (frozen pane)

Severity: HIGH Status: ✅ RESOLVED

run() iterates a poll snapshot captured before dispatch (:613-618). An earlier event in the same batch can kill a pane and close its master; a later accept or spawn in the same batch can reuse that fd number for a different registration. A subsequent stale event for the dead pane then takes the invalid-pane branch:

var pane: *Pane = @ptrCast(@alignCast(ev.udata orelse return .not_ours));
if (!self.isPaneValid(pane)) {
    self.loop.removeFd(ev.fd);        // may be the NEW owner's registration!
    return .handled;
}

Nothing re-arms the evicted fd (addFdEvents is a no-op for unregistered fds, loop.zig:71-78): a new pane permanently stops producing output; a new client wedges. The POLLNVAL branch immediately below guards precisely this hazard with if (pty.master == ev.fd) before acting (:748) — the invalid-pane branch lacks the check. Residual hole left by the #362/#364 fixes.

Fix: before removeFd, verify a live pane still owns that master (for sessions/windows/panes: pane.pty == null or pane.pty.?.master != ev.fd -> just drop the event).