Skip to content

Commit 0c1a388

Browse files
committed
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 55a1889 commit 0c1a388

File tree

2 files changed

+14
-13
lines changed

2 files changed

+14
-13
lines changed

lib/internal/bootstrap/node.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,12 @@ function startup() {
155155
return;
156156
}
157157

158-
if (isMainThread) {
158+
// If the process is spawned with env NODE_CHANNEL_FD, it's probably
159+
// spawned by our child_process module, then initialize IPC.
160+
// This attaches some internal event listeners and creates:
161+
// process.send(), process.channel, process.connected,
162+
// process.disconnect()
163+
if (isMainThread && process.env.NODE_CHANNEL_FD) {
159164
mainThreadSetup.setupChildProcessIpcChannel();
160165
}
161166

lib/internal/process/main_thread_only.js

Lines changed: 8 additions & 12 deletions
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
}
@@ -163,18 +161,16 @@ function setupSignalHandlers(internalBinding) {
163161
}
164162

165163
function setupChildProcessIpcChannel() {
166-
// If we were spawned with env NODE_CHANNEL_FD then load that up and
167-
// start parsing data from that stream.
168-
if (process.env.NODE_CHANNEL_FD) {
169-
const fd = parseInt(process.env.NODE_CHANNEL_FD, 10);
170-
assert(fd >= 0);
164+
const assert = require('assert').strict;
171165

172-
// Make sure it's not accidentally inherited by child processes.
173-
delete process.env.NODE_CHANNEL_FD;
166+
const fd = parseInt(process.env.NODE_CHANNEL_FD, 10);
167+
assert(fd >= 0);
174168

175-
require('child_process')._forkChild(fd);
176-
assert(process.send);
177-
}
169+
// Make sure it's not accidentally inherited by child processes.
170+
delete process.env.NODE_CHANNEL_FD;
171+
172+
require('child_process')._forkChild(fd);
173+
assert(process.send);
178174
}
179175

180176
module.exports = {

0 commit comments

Comments
 (0)