node-pty icon indicating copy to clipboard operation
node-pty copied to clipboard

Killing a child process returns exit code `0` instead of `null`

Open SamVerschueren opened this issue 4 years ago • 2 comments

Environment details

  • OS: macOS Big Sur
  • OS version: 11.1
  • node-pty version: 0.10.0

Issue description

I'm testing some things out with node-pty and noticed a difference between how Node.js child_process handles on exit and how node-pty handles them.

I wrote a small example.

child.js

Just a small program that logs tick every second.

setInterval(() => {
    console.log('tick');
}, 1000);

cp.js

const cp = require('child_process');

const child = cp.spawn('node', ['child.js']);

child.on('exit', (exitCode, signal) => {
    console.log(exitCode, signal);
});

child.kill('SIGINT');

When I run this code, it logs

null SIGINT

So, the exitCode is null here because it was killed with a signal like SIGINT.

pty.js

const pty = require('node-pty');

const child = pty.spawn('node', ['./child.js']);

child.onExit(({exitCode, signal}) => {
    console.log(exitCode, signal);
});

child.kill('SIGINT');

Doing the same with node-pty however, gives me the following result

0 2

2 is the numeric value of SIGINT, but the exit code is 0 indicating that the process exited just fine.

The same goes if the child.js kills itself with process.exit(1) for instance, the signal with child_process is null, but for node-pty it's 0.

I'm wondering if node-pty wants to be on par with how child_process works or if this is expected behaviour?

Thanks for the package though, it's awesome :)!

SamVerschueren avatar Feb 24 '21 13:02 SamVerschueren

I think we just pass along the code and signal, so not sure why this is happening:

https://github.com/microsoft/node-pty/blob/6cf84b7e572f2032febb5d8cec469e2f320632e0/src/unix/pty.cc#L505-L508

https://github.com/microsoft/node-pty/blob/6cf84b7e572f2032febb5d8cec469e2f320632e0/src/unixTerminal.ts#L78

Tyriar avatar May 25 '21 14:05 Tyriar

We should probably align here and hardcode null as the code when kill is called.

Tyriar avatar Oct 21 '21 19:10 Tyriar