{"record":{"id":"045a8116a3bb1aec","repo":"thedotmack/claude-mem","slug":"tree-kill-failed-for-pid-pid-error-instanceo","errorCode":null,"errorMessage":"tree-kill failed for PID ${pid}: ${error instanceof Error ? error.message : String(error)}","messagePattern":"tree-kill failed for PID (.+?): (.+?)","errorType":"exception","errorClass":"ProcessTreeKillError","httpStatus":null,"severity":"error","filePath":"src/shared/kill-process-tree.ts","lineNumber":282,"sourceCode":"        // Already dead — fine.\n      }\n    }\n    // In immediate mode the root already received SIGKILL in the pass above,\n    // and SIGKILL is not survivable — re-sending it would be pure noise (and\n    // would misrepresent the signal sequence to anything observing it).\n    if (!immediate && rootIsIntact()) {\n      try {\n        process.kill(pid, 'SIGKILL');\n      } catch {\n        // Already dead — fine.\n      }\n    }\n  } catch (error) {\n    // The individual signal calls above already tolerate ESRCH themselves, so\n    // anything surfacing here is a genuine failure of the teardown. Swallowing\n    // it would contradict this function's documented contract (and let\n    // `server stop` claim success over a kill that did not happen).\n    throw new ProcessTreeKillError(\n      pid,\n      `tree-kill failed for PID ${pid}: ${error instanceof Error ? error.message : String(error)}`,\n      { cause: error }\n    );\n  }\n}\n\n/** Descendant PID paired with the start token that proves its identity. */\nexport interface DescendantIdentity {\n  pid: number;\n  startToken: string | null;\n}\n\n/** One process-table row: discovery and identity from a SINGLE observation. */\ninterface ProcessTableRow {\n  pid: number;\n  ppid: number;\n  startToken: string | null;","sourceCodeStart":264,"sourceCodeEnd":300,"githubUrl":"https://github.com/thedotmack/claude-mem/blob/d8bc9755e74915e5c3b999181e10a67c889bce2a/src/shared/kill-process-tree.ts#L264-L300","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Inspect error.cause for the underlying failure; verify pgrep/ps are present and executable in the environment (`which pgrep`)","Confirm whether the tree survived (`ps -ef --forest | grep <pid>`); if processes remain, kill them manually with kill -9","Catch ProcessTreeKillError in callers (cleanupUnexpectedCloseSubprocess, server stop) and surface the failure instead of assuming the teardown succeeded"],"exampleFix":"// before\ndisposeCurrentSubprocess(); // never fails? it can\n\n// after\ntry {\n  await disposeCurrentSubprocess();\n} catch (e) {\n  if (e instanceof ProcessTreeKillError) logger.error('teardown failed', { pid: e.pid, cause: e.cause });\n  throw e;\n}","handlingStrategy":"try-catch","validationCode":"import { execFileSync } from 'child_process';\n// POSIX pre-check: pgrep must exist for the descendant walk to work\ntry { execFileSync('pgrep', ['--version'], { stdio: 'ignore' }); } catch {\n  console.warn('pgrep unavailable; tree-kill may fail in this environment');\n}","typeGuard":"function isProcessTreeKillError(e: unknown): e is ProcessTreeKillError {\n  return e instanceof ProcessTreeKillError;\n}","tryCatchPattern":"try {\n  await killProcessTree(pid);\n} catch (e) {\n  if (isProcessTreeKillError(e)) {\n    logger.error('teardown failed', { pid: e.pid, cause: e.cause });\n    // fall back to plain process.kill(pid, 'SIGKILL') if appropriate\n  } else throw e;\n}","preventionTips":["Ensure pgrep/ps exist in minimal containers used for claude-mem","Always await killProcessTree and inspect error.cause on failure","Verify teardown with a post-kill liveness check instead of assuming success"],"tags":["posix","process-kill","signal"],"backgroundTag":"process-kill-failed","analyzedSha":"d8bc9755e74915e5c3b999181e10a67c889bce2a","analyzedAt":"2026-09-17T16:40:26.182Z","contentChangedAt":"2026-09-17T16:40:26.182Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}