diff options
author | Ben Noordhuis <info@bnoordhuis.nl> | 2017-03-21 11:30:59 +0100 |
---|---|---|
committer | James M Snell <jasnell@gmail.com> | 2017-03-24 09:19:09 -0700 |
commit | 9ff7ed23cd822dc810ddd99d63d4e2ca68635474 (patch) | |
tree | b48ac0c73921cbbe0b69dcea2c2e06b0a859eaa6 /test/parallel/test-cli-eval.js | |
parent | 30f1e8ea041c97370534b9d26eef8f25da18a75c (diff) | |
download | android-node-v8-9ff7ed23cd822dc810ddd99d63d4e2ca68635474.tar.gz android-node-v8-9ff7ed23cd822dc810ddd99d63d4e2ca68635474.tar.bz2 android-node-v8-9ff7ed23cd822dc810ddd99d63d4e2ca68635474.zip |
lib: fix event race condition with -e
Commit c5b07d4 ("lib: fix beforeExit not working with -e") runs the
to-be-evaluated code at a later time than before because it switches
from `process.nextTick()` to `setImmediate()`.
It affects `-e 'process.on("message", ...)'` because there is now a
larger time gap between startup and attaching the event listener,
increasing the chances of missing early messages. I'm reasonably
sure `process.nextTick()` was also susceptible to that, only less
pronounced.
Avoid the problem altogether by evaluating the code synchronously.
Harmonizes the logic with `Module.runMain()` from lib/module.js
which also calls `process._tickCallback()` afterwards.
PR-URL: https://github.com/nodejs/node/pull/11958
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Reviewed-By: Daijiro Wachi <daijiro.wachi@gmail.com>
Diffstat (limited to 'test/parallel/test-cli-eval.js')
-rw-r--r-- | test/parallel/test-cli-eval.js | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/test/parallel/test-cli-eval.js b/test/parallel/test-cli-eval.js index 6d21b5d50b..34681bd235 100644 --- a/test/parallel/test-cli-eval.js +++ b/test/parallel/test-cli-eval.js @@ -171,6 +171,25 @@ child.exec(`${nodejs} --use-strict -p process.execArgv`, assert.strictEqual(proc.stdout, 'start\nbeforeExit\nexit\n'); } +// Regression test for https://github.com/nodejs/node/issues/11948. +{ + const script = ` + process.on('message', (message) => { + if (message === 'ping') process.send('pong'); + if (message === 'exit') process.disconnect(); + }); + `; + const proc = child.fork('-e', [script]); + proc.on('exit', common.mustCall((exitCode, signalCode) => { + assert.strictEqual(exitCode, 0); + assert.strictEqual(signalCode, null); + })); + proc.on('message', (message) => { + if (message === 'pong') proc.send('exit'); + }); + proc.send('ping'); +} + [ '-arg1', '-arg1 arg2 --arg3', '--', |