szn

Bug 411 — Pty pid/fd lifecycle

411. Pty pid/fd lifecycle: global reaper leaves stale Pty.pid (deinit SIGKILLs a possibly-recycled pid); Pty.deinit not idempotent on master

Severity: MEDIUM Status: ✅ RESOLVED

reapZombies drains all children with waitpid(-1, …, WNOHANG) and has no route to clear the owning Pty.pid (only Pty.reap() does). After the reaper collects a shell’s exit, the pane’s Pty still holds pid > 0, so a later Pty.deinit:

if (self.pid > 0) { _ = std.c.kill(self.pid, std.c.SIG.KILL); }

kills a pid that may have been recycled to an unrelated process (arbitrary-process kill), and the SIGHUP path’s targeted waitpid(pty.pid) (:845) gets spurious ECHILD.

Second defect: deinit does close(self.master) without setting master = -1 — unlike slave (pty.zig:270/214) and unlike the deliberate pty.master = -1 the POLLNVAL handler performs (server.zig:749) precisely to stop re-closing. Two deinits double-close, and a closed-then-recycled fd number clobbers an unrelated fd (the same class #364 defended).

Fix: map pids to panes for the reaper (or mark pid = -1 when a child is reaped by id from the pane loop); add an idempotency latch in Pty.deinit (if self.master >= 0 { close; master = -1 }).