{"record":{"id":"6785f80117895ca8","repo":"thedotmack/claude-mem","slug":"failed-to-parse-pid-file","errorCode":null,"errorMessage":"Failed to parse PID file","messagePattern":"Failed to parse PID file","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"src/services/infrastructure/ProcessManager.ts","lineNumber":146,"sourceCode":"  type PidInfo\n} from '../../supervisor/process-registry.js';\nexport { captureProcessStartToken, verifyPidFileOwnership, type PidInfo };\n\nexport function writePidFile(info: PidInfo): void {\n  mkdirSync(DATA_DIR, { recursive: true });\n  const resolvedToken = info.startToken ?? captureProcessStartToken(info.pid);\n  const payload: PidInfo = resolvedToken ? { ...info, startToken: resolvedToken } : info;\n  writeFileSync(PID_FILE, JSON.stringify(payload, null, 2));\n}\n\nexport function readPidFile(): PidInfo | null {\n  if (!existsSync(PID_FILE)) return null;\n\n  try {\n    return JSON.parse(readFileSync(PID_FILE, 'utf-8'));\n  } catch (error: unknown) {\n    if (error instanceof Error) {\n      logger.warn('SYSTEM', 'Failed to parse PID file', { path: PID_FILE }, error);\n    } else {\n      logger.warn('SYSTEM', 'Failed to parse PID file', { path: PID_FILE }, new Error(String(error)));\n    }\n    return null;\n  }\n}\n\nexport function removePidFile(): void {\n  if (!existsSync(PID_FILE)) return;\n\n  try {\n    unlinkSync(PID_FILE);\n  } catch (error: unknown) {\n    if (error instanceof Error) {\n      logger.warn('SYSTEM', 'Failed to remove PID file', { path: PID_FILE }, error);\n    } else {\n      logger.warn('SYSTEM', 'Failed to remove PID file', { path: PID_FILE }, new Error(String(error)));\n    }","sourceCodeStart":128,"sourceCodeEnd":164,"githubUrl":"https://github.com/thedotmack/claude-mem/blob/e2d1df569a8f04075d40e92461128ece7cf04c82/src/services/infrastructure/ProcessManager.ts#L128-L164","documentation":"readPidFile reads the worker's PID file and JSON.parse's it to recover the recorded pid for liveness checks and guarded removal. This warning (Error branch) fires when the file exists but its content is not valid JSON — corrupted, empty, or partially written. The function returns null, which callers treat as 'no live worker recorded', typically leading to stale-file cleanup or a fresh start.","triggerScenarios":"PID_FILE exists and JSON.parse(readFileSync(...)) throws a SyntaxError: file truncated by a crash between writeFileSync and completion, zero-byte file, or foreign content written by another tool at the same path.","commonSituations":"Process killed mid-write; disk-full truncating the file; a different program overwriting the path; a leftover file from an old version using a different format.","solutions":["Confirm no live worker owns the recorded pid (ps/Task Manager), then delete the stale PID file — it is regenerable state.","Inspect the file content; if it looks valid, the logged SyntaxError names the exact character offset of the break.","If it recurs, look for concurrent writers (two workers sharing one PID_FILE path) and give each its own data dir.","After removing the file, restart the worker so it writes a clean one."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// cheap pre-check: treat empty files as absent before parsing\nconst txt = readFileSync(PID_FILE, 'utf-8');\nif (txt.trim().length === 0) {\n  removePidFile();\n} else {\n  const info = JSON.parse(txt) as PidInfo;\n}","typeGuard":"function isPidInfo(v: unknown): v is { pid: number; startToken?: number } {\n  return typeof v === 'object' && v !== null &&\n    typeof (v as { pid?: unknown }).pid === 'number';\n}","tryCatchPattern":"try {\n  const info = readPidFile();\n  if (info && isPidInfo(info) && isAlive(info.pid)) await stopWorker(info.pid);\n} catch (e) {\n  // a null return already means 'no registered worker' — proceed with fresh start\n}","preventionTips":["Treat PID files as regenerable: verify liveness by pid, never trust file content blindly.","Give each installation its own data dir so two workers never share a PID file.","Clean stale PID files after hard kills (kill -9, power loss)."],"tags":["pid-file","json","process-management","stale-state"],"backgroundTag":"json-parse-error","analyzedSha":"e2d1df569a8f04075d40e92461128ece7cf04c82","analyzedAt":"2026-08-20T23:58:13.836Z","contentChangedAt":"2026-08-20T23:58:13.836Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}