slopus/happy · error

Failed to extract PID from tmux output: ${createResult.stdou

Error message

Failed to extract PID from tmux output: ${createResult.stdout}

What it means

After creating the tmux window with -F '#{pane_pid}', spawnInTmux parses stdout with parseInt and expects a numeric PID. If the output is empty or non-numeric (parseInt yields NaN), it throws this Error including the raw stdout, since the caller relies on the PID for process tracking.

Source

Thrown at packages/happy-cli/src/utils/tmux.ts:845

            // Add the command to run in the window (runs immediately when window is created)
            createWindowArgs.push(fullCommand);

            // Add -P flag to print the pane PID immediately
            createWindowArgs.push('-P');
            createWindowArgs.push('-F', '#{pane_pid}');

            // Create window with command and get PID immediately
            const createResult = await this.executeTmuxCommand(createWindowArgs, sessionName);

            if (!createResult || createResult.returncode !== 0) {
                throw new Error(`Failed to create tmux window: ${createResult?.stderr}`);
            }

            // Extract the PID from the output
            const panePid = parseInt(createResult.stdout.trim());
            if (isNaN(panePid)) {
                throw new Error(`Failed to extract PID from tmux output: ${createResult.stdout}`);
            }

            logger.debug(`[TMUX] Spawned command in tmux session ${sessionName}, window ${windowName}, PID ${panePid}`);

            // Return tmux session info and PID
            const sessionIdentifier: TmuxSessionIdentifier = {
                session: sessionName,
                window: windowName
            };

            return {
                success: true,
                sessionId: formatTmuxSessionIdentifier(sessionIdentifier),
                pid: panePid
            };
        } catch (error) {
            logger.debug('[TMUX] Failed to spawn in tmux:', error);
            return {

View on GitHub (pinned to b824cd0a46)

Solutions

  1. Verify `tmux display-message -p -t <target> '#{pane_pid}'` prints a number in your environment.
  2. Upgrade tmux to a version that supports the #{pane_pid} format variable.
  3. Inspect the stdout in the error message to see what was actually returned and strip noise.
  4. Disable shell/profile output that may pollute non-interactive command output.
  5. As a fallback, resolve the PID afterwards via tmux list-panes -F '#{pane_pid}'.

Example fix

// before
const panePid = parseInt(createResult.stdout.trim()); // NaN when stdout is 'PID: 12345'
// after
const match = createResult.stdout.trim().match(/(\d+)\s*$/);
if (!match) throw new Error(`No PID in tmux output: ${createResult.stdout}`);
const panePid = parseInt(match[1], 10);
Defensive patterns

Strategy: try-catch

Validate before calling

// confirm #{pane_pid} works in your tmux before relying on it
const out = await execFileP('tmux', ['display-message', '-p', '#{pane_pid}']);
if (!/^\d+$/.test(out.trim())) throw new Error(`tmux #{pane_pid} unsupported: ${out}`);

Type guard

function isNumericPid(stdout: string): boolean {
  return /^\d+$/.test(stdout.trim());
}

Try / catch

try {
  await tmux.spawnInTmux(cmd);
} catch (err) {
  if ((err as Error).message.startsWith('Failed to extract PID')) {
    const pid = await lookupPanePid(sessionName, windowName); // tmux list-panes fallback
    logger.warn(`PID fallback resolved: ${pid}`);
  } else throw err;
}

Prevention

When it happens

Trigger: The new-window command returned 0 but stdout did not contain a pane PID: unusual tmux versions/formats where #{pane_pid} isn't supported, output polluted by warnings, a format-string substitution failing, or stdout containing locale-formatted or empty text.

Common situations: Very old or patched tmux builds lacking #{pane_pid}; terminal hooks or shell rc output injected into the captured stream; tmux wrapping the PID with extra characters; running under a tmux wrapper script that alters output format.

Related errors


AI-assisted analysis of slopus/happy@b824cd0a46 (2026-08-31). Data as JSON: /api/errors/4c8e831751f136e9. Report an issue: GitHub.