Skip to content
This repository was archived by the owner on Mar 4, 2020. It is now read-only.

Commit c5b07d4

Browse files
committed
lib: fix beforeExit not working with -e
Commit 93a44d5 ("src: fix deferred events not working with -e") defers evaluation of the script to the next tick. A side effect of that change is that 'beforeExit' listeners run before the actual script. 'beforeExit' is emitted when the event loop is empty but process.nextTick() does not ref the event loop. Fix that by using setImmediate(). Because it is implemented in terms of a uv_check_t handle, it interacts with the event loop properly. Fixes: nodejs/node#8534 PR-URL: nodejs/node#8821 Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
1 parent 8e54c96 commit c5b07d4

File tree

4 files changed

+48
-25
lines changed

4 files changed

+48
-25
lines changed

lib/internal/bootstrap_node.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@
341341
// Defer evaluation for a tick. This is a workaround for deferred
342342
// events not firing when evaluating scripts from the command line,
343343
// see https://github.com/nodejs/node/issues/1600.
344-
process.nextTick(function() {
344+
setImmediate(function() {
345345
const result = module._compile(script, `${name}-wrapper`);
346346
if (process._print_eval) console.log(result);
347347
});

test/message/eval_messages.out

+16-12
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ SyntaxError: Strict mode code may not include a with statement
66
at Object.exports.runInThisContext (vm.js:*)
77
at Object.<anonymous> ([eval]-wrapper:*:*)
88
at Module._compile (module.js:*:*)
9-
at bootstrap_node.js:*:*
10-
at _combinedTickCallback (internal/process/next_tick.js:*:*)
11-
at process._tickCallback (internal/process/next_tick.js:*:*)
9+
at Immediate.<anonymous> (bootstrap_node.js:*:*)
10+
at runCallback (timers.js:*:*)
11+
at tryOnImmediate (timers.js:*:*)
12+
at processImmediate [as _immediateCallback] (timers.js:*:*)
1213
42
1314
42
1415
[eval]:1
@@ -20,9 +21,10 @@ Error: hello
2021
at Object.exports.runInThisContext (vm.js:*)
2122
at Object.<anonymous> ([eval]-wrapper:*:*)
2223
at Module._compile (module.js:*:*)
23-
at bootstrap_node.js:*:*
24-
at _combinedTickCallback (internal/process/next_tick.js:*:*)
25-
at process._tickCallback (internal/process/next_tick.js:*:*)
24+
at Immediate.<anonymous> (bootstrap_node.js:*:*)
25+
at runCallback (timers.js:*:*)
26+
at tryOnImmediate (timers.js:*:*)
27+
at processImmediate [as _immediateCallback] (timers.js:*:*)
2628
[eval]:1
2729
throw new Error("hello")
2830
^
@@ -32,9 +34,10 @@ Error: hello
3234
at Object.exports.runInThisContext (vm.js:*)
3335
at Object.<anonymous> ([eval]-wrapper:*:*)
3436
at Module._compile (module.js:*:*)
35-
at bootstrap_node.js:*:*
36-
at _combinedTickCallback (internal/process/next_tick.js:*:*)
37-
at process._tickCallback (internal/process/next_tick.js:*:*)
37+
at Immediate.<anonymous> (bootstrap_node.js:*:*)
38+
at runCallback (timers.js:*:*)
39+
at tryOnImmediate (timers.js:*:*)
40+
at processImmediate [as _immediateCallback] (timers.js:*:*)
3841
100
3942
[eval]:1
4043
var x = 100; y = x;
@@ -45,9 +48,10 @@ ReferenceError: y is not defined
4548
at Object.exports.runInThisContext (vm.js:*)
4649
at Object.<anonymous> ([eval]-wrapper:*:*)
4750
at Module._compile (module.js:*:*)
48-
at bootstrap_node.js:*:*
49-
at _combinedTickCallback (internal/process/next_tick.js:*:*)
50-
at process._tickCallback (internal/process/next_tick.js:*:*)
51+
at Immediate.<anonymous> (bootstrap_node.js:*:*)
52+
at runCallback (timers.js:*:*)
53+
at tryOnImmediate (timers.js:*:*)
54+
at processImmediate [as _immediateCallback] (timers.js:*:*)
5155
[eval]:1
5256
var ______________________________________________; throw 10
5357
^

test/message/stdin_messages.out

+16-12
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ SyntaxError: Strict mode code may not include a with statement
77
at Object.exports.runInThisContext (vm.js:*)
88
at Object.<anonymous> ([stdin]-wrapper:*:*)
99
at Module._compile (module.js:*:*)
10-
at bootstrap_node.js:*:*
11-
at _combinedTickCallback (internal/process/next_tick.js:*:*)
12-
at process._tickCallback (internal/process/next_tick.js:*:*)
10+
at Immediate.<anonymous> (bootstrap_node.js:*:*)
11+
at runCallback (timers.js:*:*)
12+
at tryOnImmediate (timers.js:*:*)
13+
at processImmediate [as _immediateCallback] (timers.js:*:*)
1314
42
1415
42
1516

@@ -22,9 +23,10 @@ Error: hello
2223
at Object.exports.runInThisContext (vm.js:*)
2324
at Object.<anonymous> ([stdin]-wrapper:*:*)
2425
at Module._compile (module.js:*:*)
25-
at bootstrap_node.js:*:*
26-
at _combinedTickCallback (internal/process/next_tick.js:*:*)
27-
at process._tickCallback (internal/process/next_tick.js:*:*)
26+
at Immediate.<anonymous> (bootstrap_node.js:*:*)
27+
at runCallback (timers.js:*:*)
28+
at tryOnImmediate (timers.js:*:*)
29+
at processImmediate [as _immediateCallback] (timers.js:*:*)
2830

2931
[stdin]:1
3032
throw new Error("hello")
@@ -35,9 +37,10 @@ Error: hello
3537
at Object.exports.runInThisContext (vm.js:*)
3638
at Object.<anonymous> ([stdin]-wrapper:*:*)
3739
at Module._compile (module.js:*:*)
38-
at bootstrap_node.js:*:*
39-
at _combinedTickCallback (internal/process/next_tick.js:*:*)
40-
at process._tickCallback (internal/process/next_tick.js:*:*)
40+
at Immediate.<anonymous> (bootstrap_node.js:*:*)
41+
at runCallback (timers.js:*:*)
42+
at tryOnImmediate (timers.js:*:*)
43+
at processImmediate [as _immediateCallback] (timers.js:*:*)
4144
100
4245

4346
[stdin]:1
@@ -49,9 +52,10 @@ ReferenceError: y is not defined
4952
at Object.exports.runInThisContext (vm.js:*)
5053
at Object.<anonymous> ([stdin]-wrapper:*:*)
5154
at Module._compile (module.js:*:*)
52-
at bootstrap_node.js:*:*
53-
at _combinedTickCallback (internal/process/next_tick.js:*:*)
54-
at process._tickCallback (internal/process/next_tick.js:*:*)
55+
at Immediate.<anonymous> (bootstrap_node.js:*:*)
56+
at runCallback (timers.js:*:*)
57+
at tryOnImmediate (timers.js:*:*)
58+
at processImmediate [as _immediateCallback] (timers.js:*:*)
5559

5660
[stdin]:1
5761
var ______________________________________________; throw 10

test/parallel/test-cli-eval.js

+15
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,18 @@ child.exec(nodejs + ` -e 'require("child_process").fork("${emptyFile}")'`,
9797
assert.equal(stdout, '');
9898
assert.equal(stderr, '');
9999
});
100+
101+
// Regression test for https://github.com/nodejs/node/issues/8534.
102+
{
103+
const script = `
104+
// console.log() can revive the event loop so we must be careful
105+
// to write from a 'beforeExit' event listener only once.
106+
process.once("beforeExit", () => console.log("beforeExit"));
107+
process.on("exit", () => console.log("exit"));
108+
console.log("start");
109+
`;
110+
const options = { encoding: 'utf8' };
111+
const proc = child.spawnSync(process.execPath, ['-e', script], options);
112+
assert.strictEqual(proc.stderr, '');
113+
assert.strictEqual(proc.stdout, 'start\nbeforeExit\nexit\n');
114+
}

0 commit comments

Comments
 (0)