thedotmack/claude-mem · error · ProcessTreeKillError

tree-kill failed for PID

Error message

tree-kill failed for PID ${pid}: ${error instanceof Error ? error.message : String(error)}

What it means

On POSIX, killProcessTree walks descendants with pgrep and signals them; the individual signal calls tolerate ESRCH internally. If anything else escapes the teardown sequence (pgrep walks failing, unexpected errors in the escalation logic), it is wrapped in ProcessTreeKillError so the documented contract holds — a genuine failure is never swallowed and `server stop` cannot claim success over a kill that did not happen.

Solutions

  1. Inspect error.cause for the underlying failure; verify pgrep/ps are present and executable in the environment (`which pgrep`)
  2. Confirm whether the tree survived (`ps -ef --forest | grep <pid>`); if processes remain, kill them manually with kill -9
  3. Catch ProcessTreeKillError in callers (cleanupUnexpectedCloseSubprocess, server stop) and surface the failure instead of assuming the teardown succeeded

Example fix

// before
disposeCurrentSubprocess(); // never fails? it can

// after
try {
  await disposeCurrentSubprocess();
} catch (e) {
  if (e instanceof ProcessTreeKillError) logger.error('teardown failed', { pid: e.pid, cause: e.cause });
  throw e;
}
Defensive patterns

Strategy: try-catch

Validate before calling

import { execFileSync } from 'child_process';
// POSIX pre-check: pgrep must exist for the descendant walk to work
try { execFileSync('pgrep', ['--version'], { stdio: 'ignore' }); } catch {
  console.warn('pgrep unavailable; tree-kill may fail in this environment');
}

Type guard

function isProcessTreeKillError(e: unknown): e is ProcessTreeKillError {
  return e instanceof ProcessTreeKillError;
}

Try / catch

try {
  await killProcessTree(pid);
} catch (e) {
  if (isProcessTreeKillError(e)) {
    logger.error('teardown failed', { pid: e.pid, cause: e.cause });
    // fall back to plain process.kill(pid, 'SIGKILL') if appropriate
  } else throw e;
}

Prevention

When it happens

Trigger: The POSIX branch's try block throws: collectDescendantIdentities / pgrep -P walks fail in an unexpected way, the tree-kill helper itself rejects, or an unanticipated error surfaces outside the per-signal ESRCH tolerance.

Common situations: Restricted environments where pgrep/ps are unavailable or permission-blocked (minimal containers, hardened seccomp profiles); processes in uninterruptible states; PID races the start-token check cannot paper over.

Related errors


AI-assisted analysis of thedotmack/claude-mem@d8bc9755e7 (2026-09-17). Data as JSON: /api/errors/045a8116a3bb1aec. Report an issue: GitHub.

Appendix: source

Thrown at src/shared/kill-process-tree.ts:282

        // Already dead — fine.
      }
    }
    // In immediate mode the root already received SIGKILL in the pass above,
    // and SIGKILL is not survivable — re-sending it would be pure noise (and
    // would misrepresent the signal sequence to anything observing it).
    if (!immediate && rootIsIntact()) {
      try {
        process.kill(pid, 'SIGKILL');
      } catch {
        // Already dead — fine.
      }
    }
  } catch (error) {
    // The individual signal calls above already tolerate ESRCH themselves, so
    // anything surfacing here is a genuine failure of the teardown. Swallowing
    // it would contradict this function's documented contract (and let
    // `server stop` claim success over a kill that did not happen).
    throw new ProcessTreeKillError(
      pid,
      `tree-kill failed for PID ${pid}: ${error instanceof Error ? error.message : String(error)}`,
      { cause: error }
    );
  }
}

/** Descendant PID paired with the start token that proves its identity. */
export interface DescendantIdentity {
  pid: number;
  startToken: string | null;
}

/** One process-table row: discovery and identity from a SINGLE observation. */
interface ProcessTableRow {
  pid: number;
  ppid: number;
  startToken: string | null;

View on GitHub (pinned to d8bc9755e7)