Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
src: emit 'beforeExit' event on process object
Browse files Browse the repository at this point in the history
Unlike the 'exit' event, this event allows the user to schedule more
work and thereby postpone the exit.  That also means that the
'beforeExit' event may be emitted many times, see the attached test
case for an example.

Refs #6305.
  • Loading branch information
bnoordhuis authored and indutny committed Feb 28, 2014
1 parent 6e1eac7 commit a2eeb43
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3415,6 +3415,19 @@ void AtExit(void (*cb)(void* arg), void* arg) {
}


void EmitBeforeExit(Environment* env) {
Context::Scope context_scope(env->context());
HandleScope handle_scope(env->isolate());
Local<Object> process_object = env->process_object();
Local<String> exit_code = FIXED_ONE_BYTE_STRING(env->isolate(), "exitCode");
Local<Value> args[] = {
FIXED_ONE_BYTE_STRING(env->isolate(), "beforeExit"),
process_object->Get(exit_code)->ToInteger()
};
MakeCallback(env, process_object, "emit", ARRAY_SIZE(args), args);
}


int EmitExit(Environment* env) {
// process.emit('exit')
HandleScope handle_scope(env->isolate());
Expand Down Expand Up @@ -3518,7 +3531,14 @@ int Start(int argc, char** argv) {
// TODO(bnoordhuis) Reorder the debugger initialization logic so it can
// be removed.
Context::Scope context_scope(env->context());
uv_run(env->event_loop(), UV_RUN_DEFAULT);
bool more;
do {
more = uv_run(env->event_loop(), UV_RUN_ONCE);
if (more == false) {
EmitBeforeExit(env);
more = uv_run(env->event_loop(), UV_RUN_NOWAIT);
}
} while (more == true);

This comment has been minimized.

Copy link
@saghul

saghul Feb 28, 2014

Member

How about:

for(;;) {
    uv_run(env->event_loop(), UV_RUN_DEFAULT);
    if (!uv_loop_alive(env->event_loop())) {
        EmitBeforeExit(env);
        if (!uv_run(env->event_loop(), UV_RUN_NOWAIT))
            break;
    }
}

PS: I typed it on GH, may have typos.

This comment has been minimized.

Copy link
@indutny

indutny Feb 28, 2014

Member

Not exactly this, but I have a fix :P

This comment has been minimized.

Copy link
@thlorenz

thlorenz Mar 30, 2015

@indutny looking into this right now, do you have any more info?
Basically adding something like @saghul suggested fixes the problem partially, i.e. this test passes then.

However the test created by @trevnorris still fails even after that fix.

I'm digging into this more and keep these notes updated. Any help/pointers appreciated.

This comment has been minimized.

Copy link
@saghul

saghul Mar 30, 2015

Member

I had a quick peek, and the test you say fails works for me here, after my suggested patch.

This comment has been minimized.

Copy link
@thlorenz

thlorenz Mar 30, 2015

Thanks @saghul will keep digging (I'm currently testing on OSX BTW).

This comment has been minimized.

Copy link
@indutny

indutny via email Mar 30, 2015

Member

This comment has been minimized.

Copy link
@thlorenz

thlorenz Mar 30, 2015

No, @indutny not fixed yet. Multiple issues related to this on io.js:

nodejs/node#1264 (comment)
nodejs/node#1152

I'm currently digging, so hit me on IRC if you wanna know what I found so far.

This comment has been minimized.

Copy link
@Fishrock123

Fishrock123 Mar 30, 2015

I think you mean nodejs/node#1151

This comment has been minimized.

Copy link
@thlorenz

thlorenz Mar 30, 2015

Probably, thanks.

This comment has been minimized.

Copy link
@thlorenz

thlorenz Mar 30, 2015

Actually @saghul I applied exactly your code and now both tests are passing for me as well.
Gonna run the test-suite now to ensure nothing else is broken, especially before-exit functionality.

This comment has been minimized.

Copy link
@thlorenz

thlorenz Mar 30, 2015

@saghul :( not quite fixed I guess

=== release test-timers-unref ===
Path: parallel/test-timers-unref
Command: out/Release/iojs /Volumes/d/dev/js/io.js/test/parallel/test-timers-unref.js
--- CRASHED ---

but getting closer.

This comment has been minimized.

Copy link
@saghul

saghul Mar 30, 2015

Member

This is my output:

 $  make test-parallel 
make -C out BUILDTYPE=Release V=1
make[1]: Entering directory '/home/saghul/src/io.js/out'
make[1]: Nothing to be done for 'all'.
make[1]: Leaving directory '/home/saghul/src/io.js/out'
ln -fs out/Release/iojs iojs
/usr/bin/python tools/test.py --mode=release parallel -J
=== release test-beforeexit-event ===                         
Path: parallel/test-beforeexit-event
set immediate
assert.js:88
  throw new assert.AssertionError({
        ^
AssertionError: 4 == 1
    at process.<anonymous> (/home/saghul/src/io.js/test/parallel/test-beforeexit-event.js:40:10)
    at emitOne (events.js:82:20)
    at process.emit (events.js:166:7)
Command: out/Release/iojs /home/saghul/src/io.js/test/parallel/test-beforeexit-event.js
[00:48|% 100|+ 752|-   1]: Done                                                
Makefile:81: recipe for target 'test-parallel' failed
make: *** [test-parallel] Error 1

I'm on GNU/Linux, FWIW.

This comment has been minimized.

Copy link
@thlorenz

thlorenz Mar 30, 2015

NM the above (deleted), was based off other branch that had commit that introduced/surfaced the above extra problem.
Nothing to do with your change.

This comment has been minimized.

Copy link
@thlorenz

thlorenz Mar 30, 2015

@saghul getting the exact same test failure as you now.

This comment has been minimized.

Copy link
@thlorenz

thlorenz Mar 30, 2015

With what's on thlorenz/timer-fix I'm passing the tests in question (the test that failed seems unrelated).

/usr/bin/python tools/test.py --mode=release message parallel sequential -J
=== release test-net-better-error-messages-port-hostname ===
Path: parallel/test-net-better-error-messages-port-hostname
Command: out/Release/iojs /Volumes/d/dev/js/io.js/test/parallel/test-net-better-error-messages-port-hostname.js
--- TIMEOUT ---
[01:46|% 100|+ 837|-   1]: Done

I'm also passing the timer test created by @bryce, but still failing @trevor's test.

code = EmitExit(env);
RunAtExit(env);
env->Dispose();
Expand Down
34 changes: 34 additions & 0 deletions test/simple/test-process-before-exit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.

var assert = require('assert');
var common = require('../common');

var N = 5;
var n = 0;

function f() {
if (++n < N) setTimeout(f, 5);
}
process.on('beforeExit', f);
process.on('exit', function() {
assert.equal(n, N + 1); // The sixth time we let it through.
});
29 changes: 29 additions & 0 deletions test/simple/test-process-exit-from-before-exit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.

var assert = require('assert');
var common = require('../common');

process.on('beforeExit', common.mustCall(function() {
setTimeout(assert.fail, 5);
process.exit(0); // Should execute immediately even if we schedule new work.
assert.fail();
}));

0 comments on commit a2eeb43

Please sign in to comment.