Child process has already exited
Production Risk
Low — track child process lifecycle state before attempting to kill.
Thrown when process.kill() or child.kill() is called on a process that has already exited. Since the process no longer exists, signals cannot be delivered to it.
- 1Calling process.kill(pid) with the PID of a process that has already terminated
- 2Sending a signal to a child process after its exit event has fired
- 3Race condition between exit detection and signal sending
Triggered when kill() is called with the PID of a process that no longer exists.
const { spawn } = require('child_process');
const child = spawn('sleep', ['0.1']);
child.on('exit', () => {
child.kill('SIGTERM'); // process already exited — throws
});expected output
Error [ERR_PROCESS_ALREADY_EXITED]: Process has exited
Fix
Check process state before sending signals
WHEN When killing child processes in async contexts
let exited = false;
child.on('exit', () => { exited = true; });
function killIfRunning(signal = 'SIGTERM') {
if (!exited) child.kill(signal);
}Why this works
Tracking the exit state prevents kill() from being called after the process terminates.
const { spawn } = require('child_process');
const child = spawn('sleep', ['0.1']);
child.on('exit', () => {
child.kill('SIGTERM'); // process already exited — throws
}); // this triggers ERR_PROCESS_ALREADY_EXITEDtry {
// operation that may throw ERR_PROCESS_ALREADY_EXITED
riskyOperation()
} catch (err) {
if (err.code === 'ERR_PROCESS_ALREADY_EXITED') {
console.error('ERR_PROCESS_ALREADY_EXITED:', err.message)
} else {
throw err
}
}// Validate inputs before calling the operation
function safe_err_process_already_exited(...args) {
// validate args here
return performOperation(...args)
}✕ Send signals to child processes in their exit handlers
The process is already gone at that point; the signal cannot be delivered.
Node.js Error Codes Documentation
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev