Skip to content

Instantly share code, notes, and snippets.

@acburdine
Created May 22, 2020 19:17
Show Gist options
  • Save acburdine/f6cf5241281ee81bfee1bab4214363de to your computer and use it in GitHub Desktop.
Save acburdine/f6cf5241281ee81bfee1bab4214363de to your computer and use it in GitHub Desktop.
stderr piping repro case

in one terminal window, run node parent.js in another, run tail -f test.log

with the cp.stderr.destroy line commented out, the node parent.js process will not automatically exit. With the cp.stderr.destroy line uncommented, the parent process correctly exits and the child process is unaffected.

const child_process = require('child_process');
const cp = child_process.spawn('node', ['test.js'], {
detached: true,
stdio: ['ignore', 'ignore', 'pipe', 'ipc']
});
console.log(`Child pid: ${cp.pid}`);
let buffer = '';
const handler = data => buffer += data.toString('utf8');
cp.stderr.on('data', handler);
cp.on('message', (msg) => {
console.log(buffer);
if (msg.started) {
cp.stderr.off('data', handler);
// leave this line commented out and the parent won't exit
// cp.stderr.destroy();
cp.disconnect();
cp.unref();
}
})
const fs = require('fs');
let count = 0;
const ws = fs.createWriteStream('test.log');
setInterval(() => {
if (count === 10) {
if (process.send) {
process.send({ started: true });
}
}
console.error(`count: ${count}`);
ws.write(`count: ${count}\n`);
count++;
}, 1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment