szn

Bug 544 — PTY leak on watchPanePty failure and missing rollback in Server.executeAction

544. Missing errdefer cleanup after pane.spawn and after window/pane creation in executeAction

Severity: HIGH
Status: ✅ RESOLVED
Files: src/server/server.zig:1021-1065, src/main.zig:531-533

Problem

  1. Missing errdefer after pane.spawn: In Server.setupPane (src/server/server.zig:1026-1027):
    try pane.spawn(shell, self.socket_path, session.id, pane.id, env_slice);
    try self.watchPanePty(pane);
    

    and in runServerDaemon (src/main.zig:531-532):

    try pane.spawn(shell, server.socket_path, s.id, pane.id, &.{});
    try server.watchPanePty(pane);
    

    pane.spawn opens the master PTY fd and forks the child shell process. If watchPanePty (self.loop.addFd) fails (e.g. TooManyFds or OOM), neither site cleans up pane.pty, leaking the master fd and leaving an unmonitored child process running.

  2. Missing window/pane rollback in Server.executeAction (src/server/server.zig:1045-1065): Unlike cmdNewWindow (src/cmd/cmd.zig:149-176) and cmdSplitWindow (src/cmd/cmd.zig:244-273), the keybinding action handlers for .new_window, .split_horizontal, and .split_vertical in Server.executeAction do not roll back the newly created window or split pane when self.setupPane(session, pane) returns an error. This leaves a dead, unspawned pane (pty == null) visible in the window or layout tree.

Proposed Fix

  1. In Server.setupPane and runServerDaemon, add:
    errdefer if (pane.pty) |*pty| {
        pty.deinit();
        pane.pty = null;
    };
    

    immediately after try pane.spawn(...).

  2. In Server.executeAction (.new_window, .split_horizontal, .split_vertical), roll back the newly added window (session.killWindow(w.id)) or pane (w.removePane(pane)) if self.setupPane(session, pane) fails, matching cmdNewWindow and cmdSplitWindow.