Skip to content
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

Pass process signals to child process? #33

Closed
bminer opened this issue Aug 18, 2011 · 10 comments
Closed

Pass process signals to child process? #33

bminer opened this issue Aug 18, 2011 · 10 comments

Comments

@bminer
Copy link

bminer commented Aug 18, 2011

It might be useful if Nodemon would pass SIGINT, etc. to the child process. Sometimes one might want to setup event handlers for various process signals. At present, I believe that Nodemon simply sends SIGTERM to the child process.

@bminer
Copy link
Author

bminer commented Aug 18, 2011

Actually, I also just noticed that when a child process hooks to process.on('SIGTERM', ...), the function does NOT get executed. Maybe this is a bug in node's child process module?

@remy
Copy link
Owner

remy commented Aug 18, 2011

It just calls the .kill method on the child process. I've just tried changing it so it passes through the signal, but it's not being caught in the child process, so unless I'm mistaken, I'm not sure I can successfully implement this.

@remy remy closed this as completed Aug 18, 2011
@bminer
Copy link
Author

bminer commented Aug 18, 2011

Actually, I'm not sure that I buy that. I think that Nodemon executes process.exit(0) before the child process actually ends. We may have to wait for the 'exit' event to occur before calling process.exit(0); in Nodemon. What do you think?

@bminer
Copy link
Author

bminer commented Aug 18, 2011

Also, I noticed that fs.unlink(flag) does not execute properly because it is asynchronous. Nodemon does process.exit() before the .monitor file is cleaned up, too.

@remy
Copy link
Owner

remy commented Aug 18, 2011

Before those process.exit(0) methods are called, the cleanup method is called. I changed my local copy to pass in the signal and then capture it in cleanup and then call the .kill method with the signal passed.

It wasn't received on my child process.

I think you're suggesting that I should wait for the child to exit before calling my own exit - but that's only going to work for the SIGINT signal - the rest are terminating commands so my script is going down either way.

I'm happy for you to add some extra event system that if the SIGINT is caught, cleanup, but wait and then exit - and I'll pull the change in.

It's fairly niché but I expect there's other developers who will want it too (if, as I assume, you do).

@remy
Copy link
Owner

remy commented Aug 18, 2011

Re: unlink - I could try switching it to unlinkSync - that should help, but this is the reason I understand that there's a tiny window to get stuff done: http://nodejs.org/docs/v0.4.11/api/process.html#event_exit_

@bminer
Copy link
Author

bminer commented Aug 18, 2011

That's not entirely true. I agree - the cleanup method is definitely called before process.exit(0) is executed, but the problem is that the cleanup() method makes an asynchronous call to kill the child process. And

Child processes, just like the parent process, can register event handlers for SIGINT, SIGTERM, or any signal. It is not limited to SIGINT. A parent process can also send the child process any signal by passing it into the kill() method.

When you say, "It wasn't received on my child process." that's probably because the parent process hits process.exit() before the child process has a chance to wrap up and die. If your child's signal event handler does something like console.log(...), the output will not be printed at all because the parent process (Nodemon) that forwards the output from the child process to the console has already called process.exit() and died.

Anyway, I can try to code something up. I've never used Git, so I suppose this is my chance to learn. :) And, yes. that's exactly what I'm suggesting - wait for the child process to exit before calling process.exit(), but again, it's not limited to SIGINT. And actually, you should also wait for the unlink of the .monitor file to occur before calling process.exit(), as well.

@remy
Copy link
Owner

remy commented Aug 18, 2011

You'll be fine with git :) The pull request stuff is built in to github - just fork this project, clone it locally and make your changes. Once done, do a git push. Then from github, send a pull request. Simple :)

One thing to watch out for, nodemon uses the USR2 signal to restart the child, and node uses USR1 in the debug environment, so I'm not sure if these will be passed down to your app. In fact, if the child starts emitting USR2 signals, it'll keep getting restarted! :)

@remy
Copy link
Owner

remy commented Aug 18, 2011

Another thing to watch out for - when you send a SIGINT to nodemon, it calls cleanup twice - once for the SIGINT and once for the process.on('exit', fn) - all towards the end of the code. If you're going to wait for the child to exit, you'll need to make sure the child is running in the first place!

@bminer
Copy link
Author

bminer commented Aug 18, 2011

I didn't bother messing with USR1 and USR2 signals. For right now, it only passes SIGINT and SIGTERM signals back to the child process.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants