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

Commit a2eeb43

Browse files
bnoordhuisindutny
authored andcommitted
src: emit 'beforeExit' event on process object
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.
1 parent 6e1eac7 commit a2eeb43

File tree

3 files changed

+84
-1
lines changed

3 files changed

+84
-1
lines changed

src/node.cc

+21-1
Original file line numberDiff line numberDiff line change
@@ -3415,6 +3415,19 @@ void AtExit(void (*cb)(void* arg), void* arg) {
34153415
}
34163416

34173417

3418+
void EmitBeforeExit(Environment* env) {
3419+
Context::Scope context_scope(env->context());
3420+
HandleScope handle_scope(env->isolate());
3421+
Local<Object> process_object = env->process_object();
3422+
Local<String> exit_code = FIXED_ONE_BYTE_STRING(env->isolate(), "exitCode");
3423+
Local<Value> args[] = {
3424+
FIXED_ONE_BYTE_STRING(env->isolate(), "beforeExit"),
3425+
process_object->Get(exit_code)->ToInteger()
3426+
};
3427+
MakeCallback(env, process_object, "emit", ARRAY_SIZE(args), args);
3428+
}
3429+
3430+
34183431
int EmitExit(Environment* env) {
34193432
// process.emit('exit')
34203433
HandleScope handle_scope(env->isolate());
@@ -3518,7 +3531,14 @@ int Start(int argc, char** argv) {
35183531
// TODO(bnoordhuis) Reorder the debugger initialization logic so it can
35193532
// be removed.
35203533
Context::Scope context_scope(env->context());
3521-
uv_run(env->event_loop(), UV_RUN_DEFAULT);
3534+
bool more;
3535+
do {
3536+
more = uv_run(env->event_loop(), UV_RUN_ONCE);
3537+
if (more == false) {
3538+
EmitBeforeExit(env);
3539+
more = uv_run(env->event_loop(), UV_RUN_NOWAIT);
3540+
}
3541+
} while (more == true);
35223542
code = EmitExit(env);
35233543
RunAtExit(env);
35243544
env->Dispose();
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
var assert = require('assert');
23+
var common = require('../common');
24+
25+
var N = 5;
26+
var n = 0;
27+
28+
function f() {
29+
if (++n < N) setTimeout(f, 5);
30+
}
31+
process.on('beforeExit', f);
32+
process.on('exit', function() {
33+
assert.equal(n, N + 1); // The sixth time we let it through.
34+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
var assert = require('assert');
23+
var common = require('../common');
24+
25+
process.on('beforeExit', common.mustCall(function() {
26+
setTimeout(assert.fail, 5);
27+
process.exit(0); // Should execute immediately even if we schedule new work.
28+
assert.fail();
29+
}));

0 commit comments

Comments
 (0)