Skip to content

Commit 6b5c962

Browse files
joyeecheungaddaleax
authored andcommitted
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: #25130 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Jeremiah Senkpiel <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
1 parent 19f1a50 commit 6b5c962

File tree

2 files changed

+14
-13
lines changed

2 files changed

+14
-13
lines changed

lib/internal/bootstrap/node.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,12 @@ function startup() {
129129
return;
130130
}
131131

132-
if (isMainThread) {
132+
// If the process is spawned with env NODE_CHANNEL_FD, it's probably
133+
// spawned by our child_process module, then initialize IPC.
134+
// This attaches some internal event listeners and creates:
135+
// process.send(), process.channel, process.connected,
136+
// process.disconnect()
137+
if (isMainThread && process.env.NODE_CHANNEL_FD) {
133138
mainThreadSetup.setupChildProcessIpcChannel();
134139
}
135140

lib/internal/process/main_thread_only.js

+8-12
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ const {
2121
getMainThreadStdio
2222
} = require('internal/process/stdio');
2323

24-
const assert = require('assert').strict;
25-
2624
function setupStdio() {
2725
setupProcessStdio(getMainThreadStdio());
2826
}
@@ -159,18 +157,16 @@ function setupSignalHandlers(internalBinding) {
159157
}
160158

161159
function setupChildProcessIpcChannel() {
162-
// If we were spawned with env NODE_CHANNEL_FD then load that up and
163-
// start parsing data from that stream.
164-
if (process.env.NODE_CHANNEL_FD) {
165-
const fd = parseInt(process.env.NODE_CHANNEL_FD, 10);
166-
assert(fd >= 0);
160+
const assert = require('assert').strict;
167161

168-
// Make sure it's not accidentally inherited by child processes.
169-
delete process.env.NODE_CHANNEL_FD;
162+
const fd = parseInt(process.env.NODE_CHANNEL_FD, 10);
163+
assert(fd >= 0);
170164

171-
require('child_process')._forkChild(fd);
172-
assert(process.send);
173-
}
165+
// Make sure it's not accidentally inherited by child processes.
166+
delete process.env.NODE_CHANNEL_FD;
167+
168+
require('child_process')._forkChild(fd);
169+
assert(process.send);
174170
}
175171

176172
module.exports = {

0 commit comments

Comments
 (0)