2019-01-15 16:12:21 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
// In worker threads, execute the script sent through the
|
|
|
|
// message port.
|
|
|
|
|
2019-04-09 09:55:53 +02:00
|
|
|
const { Object } = primordials;
|
|
|
|
|
2019-01-15 16:12:21 +01:00
|
|
|
const {
|
2019-03-08 14:25:47 +01:00
|
|
|
patchProcessObject,
|
2019-03-06 12:10:45 +01:00
|
|
|
setupCoverageHooks,
|
2019-03-26 21:03:27 +01:00
|
|
|
setupInspectorHooks,
|
2019-03-06 12:05:23 +01:00
|
|
|
setupWarningHandler,
|
2019-03-06 12:54:12 +01:00
|
|
|
setupDebugEnv,
|
2019-01-30 20:08:25 +01:00
|
|
|
initializeDeprecations,
|
2019-04-17 17:21:25 +02:00
|
|
|
initializeCJSLoader,
|
2019-01-15 16:12:21 +01:00
|
|
|
initializeESMLoader,
|
2019-01-24 19:56:33 +01:00
|
|
|
initializeFrozenIntrinsics,
|
2019-02-21 04:26:43 +01:00
|
|
|
initializeReport,
|
2019-02-10 12:09:44 +01:00
|
|
|
loadPreloadModules,
|
|
|
|
setupTraceCategoryState
|
2019-01-15 16:12:21 +01:00
|
|
|
} = require('internal/bootstrap/pre_execution');
|
|
|
|
|
|
|
|
const {
|
2019-01-15 17:08:20 +01:00
|
|
|
threadId,
|
|
|
|
getEnvMessagePort
|
2019-01-15 16:12:21 +01:00
|
|
|
} = internalBinding('worker');
|
|
|
|
|
2019-04-14 17:34:08 +02:00
|
|
|
const workerIo = require('internal/worker/io');
|
2019-01-15 16:12:21 +01:00
|
|
|
const {
|
2019-01-15 17:08:20 +01:00
|
|
|
messageTypes: {
|
|
|
|
// Messages that may be received by workers
|
|
|
|
LOAD_SCRIPT,
|
|
|
|
// Messages that may be posted from workers
|
|
|
|
UP_AND_RUNNING,
|
|
|
|
ERROR_MESSAGE,
|
|
|
|
COULD_NOT_SERIALIZE_ERROR,
|
|
|
|
// Messages that may be either received or posted
|
|
|
|
STDIO_PAYLOAD,
|
|
|
|
STDIO_WANTS_MORE_DATA,
|
|
|
|
},
|
|
|
|
kStdioWantsMoreDataCallback
|
2019-04-14 17:34:08 +02:00
|
|
|
} = workerIo;
|
2019-01-15 16:12:21 +01:00
|
|
|
|
2019-01-15 17:08:20 +01:00
|
|
|
const {
|
2019-06-15 02:07:15 +02:00
|
|
|
onGlobalUncaughtException
|
2019-01-15 17:08:20 +01:00
|
|
|
} = require('internal/process/execution');
|
|
|
|
|
|
|
|
const publicWorker = require('worker_threads');
|
2019-04-17 17:45:53 +02:00
|
|
|
const debug = require('internal/util/debuglog').debuglog('worker');
|
2019-03-06 12:54:12 +01:00
|
|
|
|
2019-04-14 22:44:02 +02:00
|
|
|
const assert = require('internal/assert');
|
|
|
|
|
2019-03-08 14:25:47 +01:00
|
|
|
patchProcessObject();
|
2019-03-26 21:03:27 +01:00
|
|
|
setupInspectorHooks();
|
2019-03-06 12:54:12 +01:00
|
|
|
setupDebugEnv();
|
|
|
|
|
2019-03-06 12:05:23 +01:00
|
|
|
setupWarningHandler();
|
|
|
|
|
2019-03-06 12:10:45 +01:00
|
|
|
// Since worker threads cannot switch cwd, we do not need to
|
|
|
|
// overwrite the process.env.NODE_V8_COVERAGE variable.
|
|
|
|
if (process.env.NODE_V8_COVERAGE) {
|
|
|
|
setupCoverageHooks(process.env.NODE_V8_COVERAGE);
|
|
|
|
}
|
|
|
|
|
2019-01-15 17:08:20 +01:00
|
|
|
debug(`[${threadId}] is setting up worker child environment`);
|
2019-01-15 16:12:21 +01:00
|
|
|
|
|
|
|
// Set up the message port and start listening
|
|
|
|
const port = getEnvMessagePort();
|
2019-01-15 17:08:20 +01:00
|
|
|
|
2019-01-31 21:03:25 +01:00
|
|
|
// If the main thread is spawned with env NODE_CHANNEL_FD, it's probably
|
|
|
|
// spawned by our child_process module. In the work threads, mark the
|
|
|
|
// related IPC properties as unavailable.
|
|
|
|
if (process.env.NODE_CHANNEL_FD) {
|
|
|
|
const workerThreadSetup = require('internal/process/worker_thread_only');
|
|
|
|
Object.defineProperty(process, 'channel', {
|
|
|
|
enumerable: false,
|
|
|
|
get: workerThreadSetup.unavailable('process.channel')
|
|
|
|
});
|
|
|
|
|
|
|
|
Object.defineProperty(process, 'connected', {
|
|
|
|
enumerable: false,
|
|
|
|
get: workerThreadSetup.unavailable('process.connected')
|
|
|
|
});
|
|
|
|
|
|
|
|
process.send = workerThreadSetup.unavailable('process.send()');
|
|
|
|
process.disconnect =
|
|
|
|
workerThreadSetup.unavailable('process.disconnect()');
|
|
|
|
}
|
|
|
|
|
2019-01-15 17:08:20 +01:00
|
|
|
port.on('message', (message) => {
|
|
|
|
if (message.type === LOAD_SCRIPT) {
|
|
|
|
const {
|
2019-04-14 17:34:08 +02:00
|
|
|
cwdCounter,
|
2019-01-15 17:08:20 +01:00
|
|
|
filename,
|
|
|
|
doEval,
|
|
|
|
workerData,
|
|
|
|
publicPort,
|
|
|
|
manifestSrc,
|
|
|
|
manifestURL,
|
|
|
|
hasStdin
|
|
|
|
} = message;
|
2019-02-10 12:09:44 +01:00
|
|
|
|
|
|
|
setupTraceCategoryState();
|
2019-02-21 04:26:43 +01:00
|
|
|
initializeReport();
|
2019-01-15 17:08:20 +01:00
|
|
|
if (manifestSrc) {
|
|
|
|
require('internal/process/policy').setup(manifestSrc, manifestURL);
|
|
|
|
}
|
2019-01-30 20:08:25 +01:00
|
|
|
initializeDeprecations();
|
2019-01-24 19:56:33 +01:00
|
|
|
initializeFrozenIntrinsics();
|
2019-04-17 17:21:25 +02:00
|
|
|
initializeCJSLoader();
|
2019-01-15 17:08:20 +01:00
|
|
|
initializeESMLoader();
|
|
|
|
loadPreloadModules();
|
|
|
|
publicWorker.parentPort = publicPort;
|
|
|
|
publicWorker.workerData = workerData;
|
|
|
|
|
2019-04-14 17:34:08 +02:00
|
|
|
// The counter is only passed to the workers created by the main thread, not
|
|
|
|
// to workers created by other workers.
|
|
|
|
let cachedCwd = '';
|
|
|
|
let lastCounter = -1;
|
|
|
|
const originalCwd = process.cwd;
|
|
|
|
|
|
|
|
process.cwd = function() {
|
|
|
|
const currentCounter = Atomics.load(cwdCounter, 0);
|
|
|
|
if (currentCounter === lastCounter)
|
|
|
|
return cachedCwd;
|
|
|
|
lastCounter = currentCounter;
|
|
|
|
cachedCwd = originalCwd();
|
|
|
|
return cachedCwd;
|
|
|
|
};
|
|
|
|
workerIo.sharedCwdCounter = cwdCounter;
|
|
|
|
|
2019-01-15 17:08:20 +01:00
|
|
|
if (!hasStdin)
|
|
|
|
process.stdin.push(null);
|
|
|
|
|
|
|
|
debug(`[${threadId}] starts worker script ${filename} ` +
|
|
|
|
`(eval = ${eval}) at cwd = ${process.cwd()}`);
|
|
|
|
port.unref();
|
|
|
|
port.postMessage({ type: UP_AND_RUNNING });
|
|
|
|
if (doEval) {
|
|
|
|
const { evalScript } = require('internal/process/execution');
|
|
|
|
evalScript('[worker eval]', filename);
|
|
|
|
} else {
|
|
|
|
process.argv[1] = filename; // script filename
|
|
|
|
require('module').runMain();
|
|
|
|
}
|
|
|
|
} else if (message.type === STDIO_PAYLOAD) {
|
|
|
|
const { stream, chunk, encoding } = message;
|
|
|
|
process[stream].push(chunk, encoding);
|
2019-04-14 22:44:02 +02:00
|
|
|
} else {
|
|
|
|
assert(
|
|
|
|
message.type === STDIO_WANTS_MORE_DATA,
|
|
|
|
`Unknown worker message type ${message.type}`
|
|
|
|
);
|
2019-01-15 17:08:20 +01:00
|
|
|
const { stream } = message;
|
|
|
|
process[stream][kStdioWantsMoreDataCallback]();
|
|
|
|
}
|
|
|
|
});
|
2019-01-15 16:12:21 +01:00
|
|
|
|
2019-06-15 02:07:15 +02:00
|
|
|
function workerOnGlobalUncaughtException(error, fromPromise) {
|
|
|
|
debug(`[${threadId}] gets uncaught exception`);
|
|
|
|
let handled = false;
|
2019-01-15 17:08:20 +01:00
|
|
|
try {
|
2019-06-15 02:07:15 +02:00
|
|
|
handled = onGlobalUncaughtException(error, fromPromise);
|
2019-01-15 17:08:20 +01:00
|
|
|
} catch (e) {
|
|
|
|
error = e;
|
|
|
|
}
|
2019-06-15 02:07:15 +02:00
|
|
|
debug(`[${threadId}] uncaught exception handled = ${handled}`);
|
2019-01-15 17:08:20 +01:00
|
|
|
|
2019-06-15 02:07:15 +02:00
|
|
|
if (!handled) {
|
2019-01-15 17:08:20 +01:00
|
|
|
let serialized;
|
|
|
|
try {
|
|
|
|
const { serializeError } = require('internal/error-serdes');
|
|
|
|
serialized = serializeError(error);
|
|
|
|
} catch {}
|
2019-06-15 02:07:15 +02:00
|
|
|
debug(`[${threadId}] uncaught exception serialized = ${!!serialized}`);
|
2019-01-15 17:08:20 +01:00
|
|
|
if (serialized)
|
|
|
|
port.postMessage({
|
|
|
|
type: ERROR_MESSAGE,
|
|
|
|
error: serialized
|
|
|
|
});
|
|
|
|
else
|
|
|
|
port.postMessage({ type: COULD_NOT_SERIALIZE_ERROR });
|
|
|
|
|
|
|
|
const { clearAsyncIdStack } = require('internal/async_hooks');
|
|
|
|
clearAsyncIdStack();
|
|
|
|
|
|
|
|
process.exit();
|
|
|
|
}
|
2019-06-15 02:07:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Patch the global uncaught exception handler so it gets picked up by
|
|
|
|
// node::errors::TriggerUncaughtException().
|
|
|
|
process._fatalException = workerOnGlobalUncaughtException;
|
2019-01-15 16:12:21 +01:00
|
|
|
|
|
|
|
markBootstrapComplete();
|
2019-01-15 17:08:20 +01:00
|
|
|
|
|
|
|
port.start();
|