{"record":{"id":"a0c994ccd1767a79","repo":"google/zx","slug":"invalid-pid-pid","errorCode":null,"errorMessage":"Invalid pid: ${pid}","messagePattern":"Invalid pid: (.+?)","errorType":"exception","errorClass":"Fail","httpStatus":null,"severity":"error","filePath":"src/core.ts","lineNumber":1065,"sourceCode":"export function cd(dir: string | ProcessOutput) {\n  if (dir instanceof ProcessOutput) {\n    dir = dir.toString().trim()\n  }\n\n  $.log({ kind: 'cd', dir, verbose: !$.quiet && $.verbose })\n  process.chdir(dir)\n  $[CWD] = process.cwd()\n}\n\nexport async function kill(\n  pid: number | `${number}`,\n  signal = $.killSignal || SIGTERM\n) {\n  if (\n    (typeof pid !== 'number' && typeof pid !== 'string') ||\n    !/^\\d+$/.test(pid as string)\n  )\n    throw new Fail(`Invalid pid: ${pid}`)\n\n  $.log({ kind: 'kill', pid, signal, verbose: !$.quiet && $.verbose })\n  if (\n    process.platform === 'win32' &&\n    (await new Promise((resolve) => {\n      cp.exec(`taskkill /pid ${pid} /t /f`, (err) => resolve(!err))\n    }))\n  )\n    return\n\n  for (const p of await ps.tree({ pid, recursive: true })) {\n    try {\n      process.kill(+p.pid, signal)\n    } catch (e) {}\n  }\n  try {\n    process.kill(-pid, signal)\n  } catch (e) {","sourceCodeStart":1047,"sourceCodeEnd":1083,"githubUrl":"https://github.com/google/zx/blob/00a2c484e219c2e84bfc3a199febf7fbce2cfbf4/src/core.ts#L1047-L1083","documentation":"Thrown by the module-level kill(pid, signal) function when pid is not a non-negative integer. The guard accepts a number or a string of digits (matching /^\\d+$/); anything else (undefined, NaN, negative number, 'abc', an object, a float) is rejected before any signal is sent.","triggerScenarios":"`kill(undefined)`; `kill(NaN)`; `kill('abc')`; `kill(-1)`; `kill(someObject)`; passing a pid read from a missing env var or JSON field; passing a process object instead of its .pid.","commonSituations":"Reading a pid from env/JSON that is absent; parseFloat on bad input yielding NaN; passing `child` instead of `child.pid`; negative pids from miscomputed values.","solutions":["Validate before calling: `if (/^\\d+$/.test(String(pid))) kill(+pid)`.","Check the source of the pid (env var, config, process object) and default/handle missing values.","For zx-spawned commands, use the instance method pp.kill() instead of the module-level kill()."],"exampleFix":"// before\nkill(maybePid)\n// after\nif (typeof maybePid === 'number' && maybePid >= 0) kill(maybePid)","handlingStrategy":"validation","validationCode":"function isPid(v: unknown): v is number | `${number}` {\n  return (typeof v === 'number' || typeof v === 'string') && /^\\d+$/.test(String(v))\n}\n\nif (!isPid(pid)) throw new TypeError(`Invalid pid: ${String(pid)}`)\nkill(pid as number)","typeGuard":"const isNumericPid = (v: unknown): v is number | `${number}` =>\n  typeof v === 'number' ? Number.isInteger(v) && v >= 0 : /^\\d+$/.test(String(v))","tryCatchPattern":"try {\n  await kill(pid as any)\n} catch (e) {\n  if (e instanceof Fail && /Invalid pid/.test(e.message)) {\n    console.error('pid was not a non-negative integer:', pid)\n  } else throw e\n}","preventionTips":["Validate pid with /^\\d+$/.test(String(pid)) before calling kill().","For zx-spawned commands, prefer pp.kill() over the module-level kill(pid).","Default missing config/env pids to a sentinel and skip the call rather than passing undefined."],"tags":["kill","validation","pid","process"],"backgroundTag":null,"analyzedSha":"00a2c484e219c2e84bfc3a199febf7fbce2cfbf4","analyzedAt":"2026-08-13T02:11:06.305Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}