diff options
author | Joyee Cheung <joyeec9h3@gmail.com> | 2018-12-19 20:07:16 +0800 |
---|---|---|
committer | Joyee Cheung <joyeec9h3@gmail.com> | 2018-12-22 17:41:18 +0800 |
commit | 0c1a38821825d4aced001e223bbba23b19b521f1 (patch) | |
tree | 9a60bf63a3cb9e25234878ad7061f80b0aeaafb4 /lib | |
parent | 55a1889af7159adaba59946a8139cfea12744819 (diff) | |
download | android-node-v8-0c1a38821825d4aced001e223bbba23b19b521f1.tar.gz android-node-v8-0c1a38821825d4aced001e223bbba23b19b521f1.tar.bz2 android-node-v8-0c1a38821825d4aced001e223bbba23b19b521f1.zip |
process: move child process IPC setup condition into node.js
Instead of branching in main_thread_only.js, move the branch on
process.env.NODE_CHANNEL_FD in node.js so it's easier to tell when
this needs to happen. Also added comments about what side effect
this causes, and lazy load `assert`.
PR-URL: https://github.com/nodejs/node/pull/25130
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/internal/bootstrap/node.js | 7 | ||||
-rw-r--r-- | lib/internal/process/main_thread_only.js | 20 |
2 files changed, 14 insertions, 13 deletions
diff --git a/lib/internal/bootstrap/node.js b/lib/internal/bootstrap/node.js index dcb0be8f7e..0f207ab660 100644 --- a/lib/internal/bootstrap/node.js +++ b/lib/internal/bootstrap/node.js @@ -155,7 +155,12 @@ function startup() { return; } - if (isMainThread) { + // If the process is spawned with env NODE_CHANNEL_FD, it's probably + // spawned by our child_process module, then initialize IPC. + // This attaches some internal event listeners and creates: + // process.send(), process.channel, process.connected, + // process.disconnect() + if (isMainThread && process.env.NODE_CHANNEL_FD) { mainThreadSetup.setupChildProcessIpcChannel(); } diff --git a/lib/internal/process/main_thread_only.js b/lib/internal/process/main_thread_only.js index 1db693f1ec..862194ae46 100644 --- a/lib/internal/process/main_thread_only.js +++ b/lib/internal/process/main_thread_only.js @@ -21,8 +21,6 @@ const { getMainThreadStdio } = require('internal/process/stdio'); -const assert = require('assert').strict; - function setupStdio() { setupProcessStdio(getMainThreadStdio()); } @@ -163,18 +161,16 @@ function setupSignalHandlers(internalBinding) { } function setupChildProcessIpcChannel() { - // If we were spawned with env NODE_CHANNEL_FD then load that up and - // start parsing data from that stream. - if (process.env.NODE_CHANNEL_FD) { - const fd = parseInt(process.env.NODE_CHANNEL_FD, 10); - assert(fd >= 0); + const assert = require('assert').strict; - // Make sure it's not accidentally inherited by child processes. - delete process.env.NODE_CHANNEL_FD; + const fd = parseInt(process.env.NODE_CHANNEL_FD, 10); + assert(fd >= 0); - require('child_process')._forkChild(fd); - assert(process.send); - } + // Make sure it's not accidentally inherited by child processes. + delete process.env.NODE_CHANNEL_FD; + + require('child_process')._forkChild(fd); + assert(process.send); } module.exports = { |