thedotmack/claude-mem · error · ProcessTreeKillError

taskkill failed for PID

Error message

taskkill failed for PID ${pid} (exit ${String(code)}): ${stderr.trim() || message}

What it means

On Windows, killProcessTree uses `taskkill /PID <pid> /T /F`. taskkill's 'process not found' outcome is tolerated, but any other failure — access denied, the 5s timeout, a wedged /T walk — is raised as a ProcessTreeKillError because the tree may still be alive and callers (e.g. `server stop`) must not report success over a failed kill.

Solutions

  1. Re-run the kill from an elevated (Administrator) shell if the stderr says access denied
  2. Check whether the PID is actually dead (`tasklist | findstr <pid>`); if it survived, kill it manually with `taskkill /PID <pid> /T /F` and investigate why it was protected
  3. Retry killProcessTree once — transient timeouts/wedged walks often succeed on a second attempt; handle ProcessTreeKillError explicitly in callers

Example fix

// before
cleanupUnexpectedCloseSubprocess(child); // assumed success

// after
try {
  await cleanupUnexpectedCloseSubprocess(child);
} catch (e) {
  if (e instanceof ProcessTreeKillError) logger.warn('tree may still be running', { pid: e.pid });
  throw e;
}
Defensive patterns

Strategy: try-catch

Validate before calling

import { execFile } from 'child_process';
// Windows pre-check: is the PID still there before attempting teardown?
execFile('tasklist', ['/FI', `PID eq ${pid}`], (err, stdout) => {
  if (!stdout.includes(String(pid))) console.log('already gone; skip taskkill');
});

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.warn(`kill may have failed for ${e.pid}; tree may still be running`, { cause: e.cause });
  } else throw e;
}

Prevention

When it happens

Trigger: process.platform === 'win32' and execFileAsync('taskkill', ...) rejects with an exit code other than the not-found code and stderr not matching the not-found pattern: access denied on a protected process, the 5000ms timeout expiring, or taskkill failing while walking the tree.

Common situations: Killing a service/elevated process from a non-elevated shell; antivirus or the OS holding the process during teardown; a deep/locked child tree making taskkill exceed its timeout.

Related errors


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

Appendix: source

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

      await execFileAsync('taskkill', ['/PID', String(pid), '/T', '/F'], {
        timeout: 5_000,
        windowsHide: true
      });
    } catch (error) {
      const code = (error as { code?: number | string }).code;
      const stderr = String((error as { stderr?: unknown }).stderr ?? '');
      const message = error instanceof Error ? error.message : String(error);

      // Already gone is the expected, tolerated case.
      if (code === TASKKILL_NOT_FOUND_EXIT || TASKKILL_NOT_FOUND_PATTERN.test(`${stderr} ${message}`)) {
        logger.debug('PROCESS', 'taskkill reported the process was already gone', { pid });
        return;
      }

      // Anything else — access denied, a timeout, a wedged /T walk — means the
      // tree may still be running. Surfacing it is the whole point: callers
      // like `server stop` must not report success over a failed kill.
      throw new ProcessTreeKillError(
        pid,
        `taskkill failed for PID ${pid} (exit ${String(code)}): ${stderr.trim() || message}`,
        { cause: error }
      );
    }
    return;
  }

  // POSIX: walk descendants recursively (bottom-up) and signal each.
  // `pkill -P <pid>` only reaches direct children, so `python` /
  // `chroma-mcp` under `uv` (grandchildren) get re-parented to init and
  // survive. We collect the full descendant set via `pgrep -P` walks before
  // signaling, so the SIGTERM phase reaches every layer
  // (CodeRabbit review on PR #2282).
  try {
    const firstSignal: NodeJS.Signals = immediate ? 'SIGKILL' : 'SIGTERM';
    const descendantsBeforeTerm = await collectDescendantIdentities(pid);
    // Signal leaves first, then the root.

View on GitHub (pinned to d8bc9755e7)