stablyai/orca · error · Error

Could not read CPU time for PID ${pid}

Error message

Could not read CPU time for PID ${pid}

What it means

Thrown by processCpuTimeMs() when `ps -o time= -p <pid>` returns empty output or a value parseProcessCpuTimeMs rejects (non-numeric time components). CPU time (HH:MM:SS or MM:SS) is converted to milliseconds to measure watchdog overhead.

Source

Thrown at config/scripts/hang-watchdog-process-metrics.mjs:77

  if (!raw.trim()) {
    return null
  }
  const parts = raw.split(':').map(Number)
  if (!parts.length || parts.some((part) => !Number.isFinite(part))) {
    return null
  }
  const seconds = parts.reduce((total, part) => total * 60 + part, 0)
  const milliseconds = seconds * 1_000
  return milliseconds >= 0 ? milliseconds : null
}

function processCpuTimeMs(pid) {
  const raw = execFileSync('ps', ['-o', 'time=', '-p', String(pid)], {
    encoding: 'utf8'
  }).trim()
  const milliseconds = parseProcessCpuTimeMs(raw)
  if (milliseconds === null) {
    throw new Error(`Could not read CPU time for PID ${pid}`)
  }
  return milliseconds
}

function combinedCpuTimeMs(pids) {
  return pids.reduce((total, pid) => total + processCpuTimeMs(pid), 0)
}

export async function sampleProductionPerformance(boundary, options) {
  const loopDelay = monitorEventLoopDelay({ resolution: 10 })
  let heartbeatCount = 0
  const heartbeat = setInterval(() => {
    heartbeatCount += 1
    boundary.sendHeartbeat()
  }, options.heartbeatIntervalMs)
  const cpuBefore = combinedCpuTimeMs(boundary.pids)
  loopDelay.enable()
  try {

View on GitHub (pinned to 1136503c6a)

Solutions

  1. Verify the PID is alive before sampling CPU time.
  2. Ensure no external signal kills the process during the sampling window.
  3. Debug with a manual `ps -o time= -p <pid>` during a trial.

Example fix

// before
const cpu = processCpuTimeMs(pid)  // throws if pid dead
// after
try { process.kill(pid, 0) } catch { throw new Error(`pid ${pid} exited before CPU sample`) }
const cpu = processCpuTimeMs(pid)
Defensive patterns

Strategy: validation

Validate before calling

function isAlive(pid: number): boolean { try { process.kill(pid, 0); return true } catch { return false } }
if (!isAlive(pid)) throw new Error(`pid ${pid} exited before CPU time sample`)

Type guard

const isProcessAlive = (pid: number): boolean => { try { process.kill(pid, 0); return true } catch { return false } }

Prevention

When it happens

Trigger: The PID exited before the read (ps returns empty for a dead PID), the time format includes unexpected tokens, or ps column output is empty for a just-started process with zero accumulated CPU time on some platforms.

Common situations: Child process exiting between the before/after CPU samples, race during shutdown, or platform ps behavior differences for processes with no accumulated time.

Related errors


AI-assisted analysis of stablyai/orca@1136503c6a (2026-08-12). Data as JSON: /api/errors/651869a4174b0fef. Report an issue: GitHub.