Child process closed before sending a reply
Production Risk
Can stall cluster master/worker coordination; always reply to IPC messages before exiting.
Thrown when a child process that was expected to send a reply via IPC closes its IPC channel before doing so. This typically happens when child_process internals are waiting for a response (e.g. from cluster workers) and the child exits unexpectedly.
- 1Child process crashes or exits before sending a required IPC reply
- 2Child process calls process.exit() inside a message handler before responding
- 3Network or OS-level process termination interrupts IPC communication
Triggered when the Node.js cluster or child_process internals detect IPC closure before receiving an expected reply.
// In a cluster worker:
process.on('message', (msg) => {
if (msg.cmd === 'handshake') {
process.exit(0); // exits without replying — causes ERR_CHILD_CLOSED_BEFORE_REPLY
}
});expected output
Error [ERR_CHILD_CLOSED_BEFORE_REPLY]: Child closed before reply received
Fix
Ensure workers always reply before exiting
WHEN In cluster workers that handle IPC messages
process.on('message', (msg) => {
if (msg.cmd === 'handshake') {
process.send({ cmd: 'handshake_ack' }); // reply first
process.exit(0);
}
});Why this works
Sending the reply before exiting ensures the parent receives the expected response.
// In a cluster worker:
process.on('message', (msg) => {
if (msg.cmd === 'handshake') {
process.exit(0); // exits without replying — causes ERR_CHILD_CLOSED_BEFORE_REPLY
}
}); // this triggers ERR_CHILD_CLOSED_BEFORE_REPLYtry {
// operation that may throw ERR_CHILD_CLOSED_BEFORE_REPLY
riskyOperation()
} catch (err) {
if (err.code === 'ERR_CHILD_CLOSED_BEFORE_REPLY') {
console.error('ERR_CHILD_CLOSED_BEFORE_REPLY:', err.message)
} else {
throw err
}
}// Validate inputs before calling the operation
function safe_err_child_closed_before_reply(...args) {
// validate args here
return performOperation(...args)
}✕ Call process.exit() inside message handlers before replying
The IPC channel closes on exit; pending replies are never delivered.
Node.js Error Codes Documentation
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev