Skip to content

Commit 9a7e883

Browse files
joyeecheungtargos
authored andcommitted
process: group main thread execution preparation code
This patch groups the preparation of main thread execution into `prepareMainThreadExecution()` and removes the cluster IPC setup in worker thread bootstrap since clusters do not use worker threads for its implementation and it's unlikely to change. PR-URL: #26000 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Richard Lau <riclau@uk.ibm.com> Reviewed-By: Minwoo Jung <minwoo@nodesource.com>
1 parent d7bf070 commit 9a7e883

9 files changed

+67
-96
lines changed

lib/internal/bootstrap/node.js

-25
Original file line numberDiff line numberDiff line change
@@ -176,31 +176,6 @@ if (config.hasInspector) {
176176
internalBinding('inspector').registerAsyncHook(enable, disable);
177177
}
178178

179-
// If the process is spawned with env NODE_CHANNEL_FD, it's probably
180-
// spawned by our child_process module, then initialize IPC.
181-
// This attaches some internal event listeners and creates:
182-
// process.send(), process.channel, process.connected,
183-
// process.disconnect()
184-
if (process.env.NODE_CHANNEL_FD) {
185-
if (ownsProcessState) {
186-
mainThreadSetup.setupChildProcessIpcChannel();
187-
} else {
188-
Object.defineProperty(process, 'channel', {
189-
enumerable: false,
190-
get: workerThreadSetup.unavailable('process.channel')
191-
});
192-
193-
Object.defineProperty(process, 'connected', {
194-
enumerable: false,
195-
get: workerThreadSetup.unavailable('process.connected')
196-
});
197-
198-
process.send = workerThreadSetup.unavailable('process.send()');
199-
process.disconnect =
200-
workerThreadSetup.unavailable('process.disconnect()');
201-
}
202-
}
203-
204179
const browserGlobals = !process._noBrowserGlobals;
205180
if (browserGlobals) {
206181
setupGlobalTimeouts();

lib/internal/bootstrap/pre_execution.js

+37-5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,27 @@
22

33
const { getOptionValue } = require('internal/options');
44

5+
function prepareMainThreadExecution() {
6+
// If the process is spawned with env NODE_CHANNEL_FD, it's probably
7+
// spawned by our child_process module, then initialize IPC.
8+
// This attaches some internal event listeners and creates:
9+
// process.send(), process.channel, process.connected,
10+
// process.disconnect().
11+
setupChildProcessIpcChannel();
12+
13+
// Load policy from disk and parse it.
14+
initializePolicy();
15+
16+
// If this is a worker in cluster mode, start up the communication
17+
// channel. This needs to be done before any user code gets executed
18+
// (including preload modules).
19+
initializeClusterIPC();
20+
21+
initializeDeprecations();
22+
initializeESMLoader();
23+
loadPreloadModules();
24+
}
25+
526
// In general deprecations are intialized wherever the APIs are implemented,
627
// this is used to deprecate APIs implemented in C++ where the deprecation
728
// utitlities are not easily accessible.
@@ -41,10 +62,22 @@ function initializeDeprecations() {
4162
}
4263
}
4364

65+
function setupChildProcessIpcChannel() {
66+
if (process.env.NODE_CHANNEL_FD) {
67+
const assert = require('internal/assert');
68+
69+
const fd = parseInt(process.env.NODE_CHANNEL_FD, 10);
70+
assert(fd >= 0);
71+
72+
// Make sure it's not accidentally inherited by child processes.
73+
delete process.env.NODE_CHANNEL_FD;
74+
75+
require('child_process')._forkChild(fd);
76+
assert(process.send);
77+
}
78+
}
79+
4480
function initializeClusterIPC() {
45-
// If this is a worker in cluster mode, start up the communication
46-
// channel. This needs to be done before any user code gets executed
47-
// (including preload modules).
4881
if (process.argv[1] && process.env.NODE_UNIQUE_ID) {
4982
const cluster = require('cluster');
5083
cluster._setupWorker();
@@ -114,9 +147,8 @@ function loadPreloadModules() {
114147
}
115148

116149
module.exports = {
150+
prepareMainThreadExecution,
117151
initializeDeprecations,
118-
initializeClusterIPC,
119-
initializePolicy,
120152
initializeESMLoader,
121153
loadPreloadModules
122154
};

lib/internal/main/check_syntax.js

+2-10
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,7 @@
44
// instead of actually running the file.
55

66
const {
7-
initializeDeprecations,
8-
initializeClusterIPC,
9-
initializePolicy,
10-
initializeESMLoader,
11-
loadPreloadModules
7+
prepareMainThreadExecution
128
} = require('internal/bootstrap/pre_execution');
139

1410
const {
@@ -22,11 +18,7 @@ const {
2218
} = require('internal/modules/cjs/helpers');
2319

2420
// TODO(joyeecheung): not every one of these are necessary
25-
initializeDeprecations();
26-
initializeClusterIPC();
27-
initializePolicy();
28-
initializeESMLoader();
29-
loadPreloadModules();
21+
prepareMainThreadExecution();
3022
markBootstrapComplete();
3123

3224
if (process.argv[1] && process.argv[1] !== '-') {

lib/internal/main/eval_stdin.js

+2-10
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,15 @@
33
// Stdin is not a TTY, we will read it and execute it.
44

55
const {
6-
initializeDeprecations,
7-
initializeClusterIPC,
8-
initializePolicy,
9-
initializeESMLoader,
10-
loadPreloadModules
6+
prepareMainThreadExecution
117
} = require('internal/bootstrap/pre_execution');
128

139
const {
1410
evalScript,
1511
readStdin
1612
} = require('internal/process/execution');
1713

18-
initializeDeprecations();
19-
initializeClusterIPC();
20-
initializePolicy();
21-
initializeESMLoader();
22-
loadPreloadModules();
14+
prepareMainThreadExecution();
2315
markBootstrapComplete();
2416

2517
readStdin((code) => {

lib/internal/main/eval_string.js

+2-10
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,13 @@
44
// `--interactive`.
55

66
const {
7-
initializeDeprecations,
8-
initializeClusterIPC,
9-
initializePolicy,
10-
initializeESMLoader,
11-
loadPreloadModules
7+
prepareMainThreadExecution
128
} = require('internal/bootstrap/pre_execution');
139
const { evalScript } = require('internal/process/execution');
1410
const { addBuiltinLibsToObject } = require('internal/modules/cjs/helpers');
1511

1612
const source = require('internal/options').getOptionValue('--eval');
17-
initializeDeprecations();
18-
initializeClusterIPC();
19-
initializePolicy();
20-
initializeESMLoader();
21-
loadPreloadModules();
13+
prepareMainThreadExecution();
2214
addBuiltinLibsToObject(global);
2315
markBootstrapComplete();
2416
evalScript('[eval]', source, process._breakFirstLine);

lib/internal/main/repl.js

+2-10
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,14 @@
44
// the main module is not specified and stdin is a TTY.
55

66
const {
7-
initializeDeprecations,
8-
initializeClusterIPC,
9-
initializePolicy,
10-
initializeESMLoader,
11-
loadPreloadModules
7+
prepareMainThreadExecution
128
} = require('internal/bootstrap/pre_execution');
139

1410
const {
1511
evalScript
1612
} = require('internal/process/execution');
1713

18-
initializeDeprecations();
19-
initializeClusterIPC();
20-
initializePolicy();
21-
initializeESMLoader();
22-
loadPreloadModules();
14+
prepareMainThreadExecution();
2315

2416
const cliRepl = require('internal/repl');
2517
cliRepl.createInternalRepl(process.env, (err, repl) => {

lib/internal/main/run_main_module.js

+2-10
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,10 @@
11
'use strict';
22

33
const {
4-
initializeDeprecations,
5-
initializeClusterIPC,
6-
initializePolicy,
7-
initializeESMLoader,
8-
loadPreloadModules
4+
prepareMainThreadExecution
95
} = require('internal/bootstrap/pre_execution');
106

11-
initializeDeprecations();
12-
initializeClusterIPC();
13-
initializePolicy();
14-
initializeESMLoader();
15-
loadPreloadModules();
7+
prepareMainThreadExecution();
168

179
// Expand process.argv[1] into a full path.
1810
const path = require('path');

lib/internal/main/worker_thread.js

+20-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
const {
77
initializeDeprecations,
8-
initializeClusterIPC,
98
initializeESMLoader,
109
loadPreloadModules
1110
} = require('internal/bootstrap/pre_execution');
@@ -42,6 +41,26 @@ debug(`[${threadId}] is setting up worker child environment`);
4241
// Set up the message port and start listening
4342
const port = getEnvMessagePort();
4443

44+
// If the main thread is spawned with env NODE_CHANNEL_FD, it's probably
45+
// spawned by our child_process module. In the work threads, mark the
46+
// related IPC properties as unavailable.
47+
if (process.env.NODE_CHANNEL_FD) {
48+
const workerThreadSetup = require('internal/process/worker_thread_only');
49+
Object.defineProperty(process, 'channel', {
50+
enumerable: false,
51+
get: workerThreadSetup.unavailable('process.channel')
52+
});
53+
54+
Object.defineProperty(process, 'connected', {
55+
enumerable: false,
56+
get: workerThreadSetup.unavailable('process.connected')
57+
});
58+
59+
process.send = workerThreadSetup.unavailable('process.send()');
60+
process.disconnect =
61+
workerThreadSetup.unavailable('process.disconnect()');
62+
}
63+
4564
port.on('message', (message) => {
4665
if (message.type === LOAD_SCRIPT) {
4766
const {
@@ -57,7 +76,6 @@ port.on('message', (message) => {
5776
require('internal/process/policy').setup(manifestSrc, manifestURL);
5877
}
5978
initializeDeprecations();
60-
initializeClusterIPC();
6179
initializeESMLoader();
6280
loadPreloadModules();
6381
publicWorker.parentPort = publicPort;

lib/internal/process/main_thread_only.js

-14
Original file line numberDiff line numberDiff line change
@@ -152,22 +152,8 @@ function createSignalHandlers() {
152152
};
153153
}
154154

155-
function setupChildProcessIpcChannel() {
156-
const assert = require('internal/assert');
157-
158-
const fd = parseInt(process.env.NODE_CHANNEL_FD, 10);
159-
assert(fd >= 0);
160-
161-
// Make sure it's not accidentally inherited by child processes.
162-
delete process.env.NODE_CHANNEL_FD;
163-
164-
require('child_process')._forkChild(fd);
165-
assert(process.send);
166-
}
167-
168155
module.exports = {
169156
wrapProcessMethods,
170157
createSignalHandlers,
171-
setupChildProcessIpcChannel,
172158
wrapPosixCredentialSetters
173159
};

0 commit comments

Comments
 (0)