Skip to content

Commit 25c3207

Browse files
bnoordhuisMyles Borins
authored andcommitted
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. Ref: nodejs#9680 Fixes: nodejs#8534 PR-URL: nodejs#8821 Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
1 parent 91fce10 commit 25c3207

4 files changed

Lines changed: 32 additions & 25 deletions

File tree

src/node.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,7 @@
591591
// Defer evaluation for a tick. This is a workaround for deferred
592592
// events not firing when evaluating scripts from the command line,
593593
// see https://github.com/nodejs/node/issues/1600.
594-
process.nextTick(function() {
594+
setImmediate(function() {
595595
var result = module._compile(script, `${name}-wrapper`);
596596
if (process._print_eval) console.log(result);
597597
});

test/message/eval_messages.out

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ 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 node.js:*:*
10-
at nextTickCallbackWith0Args (node.js:*:*)
11-
at process._tickCallback (node.js:*:*)
9+
at Immediate._onImmediate (node.js:*:*)
10+
at processImmediate [as _immediateCallback] (timers.js:*:*)
1211
42
1312
42
1413
[eval]:1
@@ -19,9 +18,8 @@ Error: hello
1918
at Object.exports.runInThisContext (vm.js:*)
2019
at Object.<anonymous> ([eval]-wrapper:*:*)
2120
at Module._compile (module.js:*:*)
22-
at node.js:*:*
23-
at nextTickCallbackWith0Args (node.js:*:*)
24-
at process._tickCallback (node.js:*:*)
21+
at Immediate._onImmediate (node.js:*:*)
22+
at processImmediate [as _immediateCallback] (timers.js:*:*)
2523
[eval]:1
2624
throw new Error("hello")
2725
^
@@ -30,9 +28,8 @@ Error: hello
3028
at Object.exports.runInThisContext (vm.js:*)
3129
at Object.<anonymous> ([eval]-wrapper:*:*)
3230
at Module._compile (module.js:*:*)
33-
at node.js:*:*
34-
at nextTickCallbackWith0Args (node.js:*:*)
35-
at process._tickCallback (node.js:*:*)
31+
at Immediate._onImmediate (node.js:*:*)
32+
at processImmediate [as _immediateCallback] (timers.js:*:*)
3633
100
3734
[eval]:1
3835
var x = 100; y = x;
@@ -42,9 +39,8 @@ ReferenceError: y is not defined
4239
at Object.exports.runInThisContext (vm.js:*)
4340
at Object.<anonymous> ([eval]-wrapper:*:*)
4441
at Module._compile (module.js:*:*)
45-
at node.js:*:*
46-
at nextTickCallbackWith0Args (node.js:*:*)
47-
at process._tickCallback (node.js:*:*)
42+
at Immediate._onImmediate (node.js:*:*)
43+
at processImmediate [as _immediateCallback] (timers.js:*:*)
4844
[eval]:1
4945
var ______________________________________________; throw 10
5046
^

test/message/stdin_messages.out

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ 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 node.js:*:*
11-
at nextTickCallbackWith0Args (node.js:*:*)
12-
at process._tickCallback (node.js:*:*)
10+
at Immediate._onImmediate (node.js:*:*)
11+
at processImmediate [as _immediateCallback] (timers.js:*:*)
1312
42
1413
42
1514

@@ -21,9 +20,8 @@ Error: hello
2120
at Object.exports.runInThisContext (vm.js:*)
2221
at Object.<anonymous> ([stdin]-wrapper:*:*)
2322
at Module._compile (module.js:*:*)
24-
at node.js:*:*
25-
at nextTickCallbackWith0Args (node.js:*:*)
26-
at process._tickCallback (node.js:*:*)
23+
at Immediate._onImmediate (node.js:*:*)
24+
at processImmediate [as _immediateCallback] (timers.js:*:*)
2725

2826
[stdin]:1
2927
throw new Error("hello")
@@ -33,9 +31,8 @@ Error: hello
3331
at Object.exports.runInThisContext (vm.js:*)
3432
at Object.<anonymous> ([stdin]-wrapper:*:*)
3533
at Module._compile (module.js:*:*)
36-
at node.js:*:*
37-
at nextTickCallbackWith0Args (node.js:*:*)
38-
at process._tickCallback (node.js:*:*)
34+
at Immediate._onImmediate (node.js:*:*)
35+
at processImmediate [as _immediateCallback] (timers.js:*:*)
3936
100
4037

4138
[stdin]:1
@@ -46,9 +43,8 @@ ReferenceError: y is not defined
4643
at Object.exports.runInThisContext (vm.js:*)
4744
at Object.<anonymous> ([stdin]-wrapper:*:*)
4845
at Module._compile (module.js:*:*)
49-
at node.js:*:*
50-
at nextTickCallbackWith0Args (node.js:*:*)
51-
at process._tickCallback (node.js:*:*)
46+
at Immediate._onImmediate (node.js:*:*)
47+
at processImmediate [as _immediateCallback] (timers.js:*:*)
5248

5349
[stdin]:1
5450
var ______________________________________________; throw 10

test/parallel/test-cli-eval.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,18 @@ child.exec(nodejs + ` -e 'require("child_process").fork("${emptyFile}")'`,
9090
assert.equal(stdout, '');
9191
assert.equal(stderr, '');
9292
});
93+
94+
// Regression test for https://github.com/nodejs/node/issues/8534.
95+
{
96+
const script = `
97+
// console.log() can revive the event loop so we must be careful
98+
// to write from a 'beforeExit' event listener only once.
99+
process.once("beforeExit", () => console.log("beforeExit"));
100+
process.on("exit", () => console.log("exit"));
101+
console.log("start");
102+
`;
103+
const options = { encoding: 'utf8' };
104+
const proc = child.spawnSync(process.execPath, ['-e', script], options);
105+
assert.strictEqual(proc.stderr, '');
106+
assert.strictEqual(proc.stdout, 'start\nbeforeExit\nexit\n');
107+
}

0 commit comments

Comments
 (0)