-
Notifications
You must be signed in to change notification settings - Fork 233
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
process.on('exit')
is disabled after spawn
a child process.
#190
Comments
@zhaoyi0113 what platform? |
I am on Mac 10.13.3, node 8.9.1. Thanks. |
I did some more tests on that and found if there is a const { spawn } = require('node-pty');
const handleShutdown = (e, n) => {
console.log('exit', e, n);
process.exit(e);
};
process.on('exit', handleShutdown);
process.on('SIGINT', handleShutdown);
process.on('SIGHUP', handleShutdown);
const p = spawn('mongo', ['mongodb://localhost:27017/admin']);
p.on('data', d => console.log(d));
p.write('show dbs\n');
process.on('exit', handleShutdown);
process.on('SIGINT', handleShutdown);
process.on('SIGHUP', handleShutdown); The |
Isn't this because you're listening to |
Yes, this is what I want to listen. I want to listen on the |
You have a reference to p.on('exit', handleShutdown); |
In my application, it will spawn many processes and I have exit listener listening to each of them ( But I also need a listener who is listening to the node process exit event and do some logic there. I wonder why the |
@Tyriar Could you let me know why you closed this issue? It is an obvious issue in node-pty. Let me know if you need more explanation or I can create a reproducible repo for you to test. Thanks. |
@zhaoyi0113 because I don't think it is an obvious issue with node-pty. I don't know enough about node's lifecycle to know for sure but I think listeners/resources are sticking around so node doesn't think it should exit. You could try release the reference to |
@Tyriar My case is that I added an exit listener on node outer process to do some cleanup work. This listener is called when the outer process exit or crash or is killed for some reasons. It works fine until I spawn a node-pty process inside my node process. The node-pty child process removes the listeners I added to the outer process. So when my process crash or is killed, these listeners are not called therefore I don't have a chance to do the cleanup work. I understand that you want me to listen on the |
@zhaoyi0113 why can't you keep a stack of processes and trigger clean up/process.exit when the stack reaches 0? (or when you're done) Also make sure you try this:
Having said all this, I would accept PRs to better clean up anything inside a |
@Tyriar The problem is that once I spawn a new sub process through node-pty, the process.on('exit') is not being called when the outer process exits. Any my clean up code has to be run when the outer process exits but not sub-process exit. |
@Tyriar have exactly same issue. I have node process which manages a lot of child processed which are spawned via |
@shir handle the pty.on('exit', (code, signal) => {
...
}); |
@Tyriar I think the problem is how to listen on process exit rather than listening to pty exit. |
I run this piece of code and it exits correctly on Linux? var os = require('os');
var pty = require('node-pty');
var shell = os.platform() === 'win32' ? 'powershell.exe' : 'bash';
var ptyProcess = pty.spawn(shell, [], {
name: 'xterm-color',
cols: 80,
rows: 30,
cwd: process.env.HOME,
env: process.env
});
ptyProcess.on('data', function(data) {
console.log(data);
});
ptyProcess.write('ls\r');
ptyProcess.write('exit\r');
process.on('exit', c => console.log('exit ' + c)); |
If you remove the line |
@zhaoyi0113 works for me: process.on('SIGINT', () => {
console.log('cancel SIGINT');
}); |
@Tyriar The problem happens if you add this listener before spawn pty process. Below code doesn't work for me on both ubuntu and mac.
|
./node-pty/src/unix/pty.cc PtyFork calls signal(SIGINT, SIG_DFL) before forking, removing the signal handler in the parent process that was installed before then. |
That line was added in 56fc4a4 and is not present in the original pty.js that this was forked off of. I also notice the man page says
@jerch any insights here? I haven't touched this code path personally. |
Upps yes, |
sigaction or signal, I still think you need to move whatever that line becomes to "after" the fork. |
Not sure why it is there in the first place, all I know is that libuv's event loop and callback system was not fork save in earlier releases, but they worked on a fix around node v6. I think we should have a look at libuv's spawn implementation to decide where this should go or whether it can be removed. edit: Btw there is another signal related issue - |
+1 here this happens even when all the processes from node-pty are killed |
Tried to come up with a signal safe version in #218. |
@jerch Is the version released? It happens on the version |
Nope still in PR stage, just wrote a few tests, not sure they will cover it correctly (hard to test since SIGINT will just end the mocha process). |
Hello, I was banging my head over this problem and thanks, I saw this issue. Any update on this please? Help would be appreciated. |
Any fixes? |
I am using
node-pty
to spawn a child process as below:when I terminate the node process, the
handleShutdown
is not being called. But if I move theprocess.on
afterspawn
, thehandleShutdown
is called on termination. I think thespawn
override process listeners for some reasons. How can I fix that?The text was updated successfully, but these errors were encountered: