Skip to content

Commit

Permalink
process: move child process IPC setup condition into node.js
Browse files Browse the repository at this point in the history
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: #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>
  • Loading branch information
joyeecheung committed Dec 22, 2018
1 parent 55a1889 commit 0c1a388
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
7 changes: 6 additions & 1 deletion lib/internal/bootstrap/node.js
Expand Up @@ -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();
}

Expand Down
20 changes: 8 additions & 12 deletions lib/internal/process/main_thread_only.js
Expand Up @@ -21,8 +21,6 @@ const {
getMainThreadStdio
} = require('internal/process/stdio');

const assert = require('assert').strict;

function setupStdio() {
setupProcessStdio(getMainThreadStdio());
}
Expand Down Expand Up @@ -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 = {
Expand Down

0 comments on commit 0c1a388

Please sign in to comment.