Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nodejs child process exit before stdio streams close

I've just been experimenting with child processes and noticed that the exit event fires before the close event - the following code throws an error because this._instance.stdin no longer exists (this._instance is already null).

'use strict';

const spawn = require('child_process').spawn;

class Foo {
  constructor() {
    this._instance = null;
  }

  bar() {
    this._instance = spawn('ls', ['-l']);

    this._instance.on('close', (code, signal) => {
      this._instance.stdin.end();
    });

    this._instance.on('exit', (code, signal) => {
      this._instance = null;
    });

    return this._instance;
  }
}


var foo = new Foo();

console.log(foo.bar());

The documentation states:

"Note that when the 'exit' event is triggered, child process stdio streams might still be open."

I wondered how this happens, why do the streams still exists after the process has exited? And how do they get 'closed', is this part handled by the OS or does node do the closing of the left over stdio streams?

In practice I wouldn't necessarily set this._instance to null on exit as it doesn't seem like a nice thing to do and is obviously a bit premature.

like image 945
gratz Avatar asked May 30 '26 03:05

gratz


1 Answers

The documentation of the close event sheds some light on why this might happen.

In short, the stdio can be used by other processes which did not exit yet.

I haven't looked into the code itself, but it would make sense that the OS handles the part of closing the stdio streams. Think about piping stdio into multiple processes (piping with tee might be a good example).

In the presented case, I would suspect that you don't even need to end() stdin, as the close event suggests that the stdin stream has already been closed.

like image 171
Narigo Avatar answered Jun 01 '26 21:06

Narigo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!