-
Notifications
You must be signed in to change notification settings - Fork 7.3k
Conversation
Useful to know when the the event loop is empty, this can't be done with uv_run() without possibly blocking, or running some events (which might empty the event loop as a side-effect).
Thank you for contributing this pull request! Here are a few pointers to make sure your submission will be considered for inclusion. The following commiters were not found in the CLA:
Please see CONTRIBUTING.md for more information |
@@ -3033,6 +3033,18 @@ void EmitExit(v8::Handle<v8::Object> process_l) { | |||
MakeCallback(process, "emit", ARRAY_SIZE(args), args); | |||
} | |||
|
|||
void EmitKeepAlive(v8::Handle<v8::Object> process_l) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't need the extra v8::
here.
I do have some reservations about this. It's fairly trivial to prevent the process from ever exiting: process.on('keepalive', function() {
console.log('about to die');
}); Why couldn't you listen for |
The option to keep the event loop alive is specifically what this is meant to provide, whereas you're not supposed to do anything async in We talked about this briefly today, it was suggested that the name could be |
ah yeah. duh. |
My only real follow up has to do with the interaction of That seems like the right behavior, but it should probably be documented and perhaps tested for. It may be that for consistency that this event should accept an argument indicating this is being fired in a forceful shutdown. Just food for thought. |
for these, going to use here are a few cases that might throw devs off that may need to be documented: // using `on` instead of `once` for things like `log` that
// devs may not expect
process.on('beforeExit', function() {
console.log('never ending loop');
});
// beforeExit won't fire for either
process.once('beforeExit', function() { });
process.exit();
// or
throw Error('hello'); Also, i'm not sure that I'd agree that process.on('beforeExit', function() {
process.nextTick(function() { /* never ending loop */ });
}); The reason this happens is because |
Thank you both for the feedback. @tjfontaine I can add test for process.exit() not giving a chance to keep node alive. I think that is the predictable and desirable behaviour. @trevnorris It wouldn't have occurred to me that process.nextTick() would behave any differently from setImmediate(), or net.connect(), so I'm glad it doesn't. Its an async call, it got scheduled, so it keeps node alive. I can mention more explicitly that calling any asynchronous function will cause node to continue instead of exiting, and use console.log as an example of a function that is sometimes async. Btw, I'm fine with beforeExit, but I'm not the one who is going to have to close the bug reports claiming it "doesn't fire before exit" (process.exit(), throw, etc.). That's why I suggested keepAlive, dead, quiet, loopQuiet, loopEmpty, or something like that, they seem less misleading. Its also why I tried to highlight in the docs that it is not a replacement for the exit event. I'll rewrite this to beforeExit tomorrow if there are no other comments, and add some more tests. |
good point. let's get some more feedback there. /cc @isaacs |
Discussed with @tjfontaine and @trevnorris in IRC. Here are some decisions we've come to:
|
Well... process.stdout is async when it's a pipe. |
@isaacs yes, the reason for function onwrite(stream, er) {
// ...
if (sync) {
process.nextTick(function() {
afterWrite(stream, state, finished, cb);
});
} else {
afterWrite(stream, state, finished, cb);
} @sam-github because of the coming change to |
@bnoordhuis would that be changed by #5595? |
On Windows maybe, on Unices we want to keep the current behavior. |
If the event listener schedules more work, the loop will be restarted, otherwise, exit proceeds as usual. Test immediates, timers, and listening sockets will keep node alive, nextTick should not keep node alive, after upcoming changes in tick handling.
I renamed, resynced code with process.emit('exit'), tested process.exit() doesn't allow restart, tested 3 variants of work (immediates, timers, server sockets), rewrote the docs, and explicitly don't depend on nextTick keeping the loop alive. I think that addresses all the comments, except mine ;-)
|
Closing, no longer applies. Superseded by #6316. |
This was partially spurred by unit tests silently exiting. The code under test was buggy, it failed to schedule any work, but the result was the test runner bailing with exit 0, and no clear indication in the output what had happened. This event would allow timer-less detection of the exit condition.
Possible names for this event were:
I'm open to suggestions if this is considered useful.
I didn't know how to structure a node pull request that will fail to build without a libuv upgrade, so I applied joyent/libuv#813 to deps/uv and used that as a base.