2022-07-05 16:30:52 -06:00
|
|
|
'use strict';
|
|
|
|
|
2022-09-02 19:49:06 -06:00
|
|
|
// Generic `child_process` runner that handles running the given module with the
|
|
|
|
// given `runArguments` and returning the async result. Handles the complexity
|
|
|
|
// error handling, passing large argument objects, and timeouts.
|
|
|
|
//
|
|
|
|
// Error handling includes main-line errors seen while waiting the async result,
|
|
|
|
// as well as keeping track of out of band `uncaughtException` and
|
|
|
|
// `unhandledRejection` to give more context if the process exits with code 1
|
|
|
|
// (error) or timesout.
|
2022-07-05 16:30:52 -06:00
|
|
|
|
2022-09-02 19:49:06 -06:00
|
|
|
const assert = require('assert');
|
2022-09-08 00:30:04 -06:00
|
|
|
const { fork } = require('child_process');
|
2022-07-05 16:30:52 -06:00
|
|
|
|
2023-05-01 23:39:01 -06:00
|
|
|
const RethrownError = require('../lib/errors/rethrown-error');
|
2022-08-29 13:13:13 -06:00
|
|
|
const { traceFunction } = require('../tracing/trace-utilities');
|
2022-07-05 16:30:52 -06:00
|
|
|
|
2022-08-29 18:13:56 -06:00
|
|
|
const config = require('../lib/config');
|
|
|
|
const logOutputFromChildProcesses = config.get('logOutputFromChildProcesses');
|
|
|
|
|
|
|
|
if (!logOutputFromChildProcesses) {
|
|
|
|
console.warn(
|
|
|
|
`Silencing logs from child processes (config.logOutputFromChildProcesses = ${logOutputFromChildProcesses})`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-04-24 23:50:43 -06:00
|
|
|
const resolvedChildForkScriptPath = require.resolve('./child-fork-script');
|
|
|
|
|
2023-05-01 23:39:01 -06:00
|
|
|
class RunInChildProcessTimeoutAbortError extends RethrownError {
|
|
|
|
// ...
|
|
|
|
}
|
|
|
|
|
2023-05-01 16:33:48 -06:00
|
|
|
function assembleErrorAfterChildExitsWithErrors(exitCode, childErrors, childStdErr) {
|
2022-09-02 17:49:45 -06:00
|
|
|
assert(childErrors);
|
|
|
|
|
|
|
|
let extraErrorsMessage = '';
|
|
|
|
if (childErrors.length > 1) {
|
|
|
|
extraErrorsMessage = ` (somehow we saw ${
|
|
|
|
childErrors.length
|
|
|
|
} errors but we really always expect 1 error)\n${childErrors
|
|
|
|
.map((childError, index) => `${index}. ${childError.stack}`)
|
|
|
|
.join('\n')}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
let childErrorToDisplay;
|
|
|
|
if (childErrors.length === 0) {
|
2023-05-01 16:33:48 -06:00
|
|
|
childErrorToDisplay = new Error(
|
|
|
|
`No child errors but there might be something in stderr=${childStdErr}`
|
|
|
|
);
|
2022-09-02 17:49:45 -06:00
|
|
|
// Clear the stack trace part of the stack string out because this is just a
|
|
|
|
// note about the lack of errors, not an actual error and is just noisy with
|
|
|
|
// that extra fluff.
|
|
|
|
childErrorToDisplay.stack = childErrorToDisplay.message;
|
|
|
|
} else if (childErrors.length === 1) {
|
|
|
|
childErrorToDisplay = childErrors[0];
|
|
|
|
} else {
|
|
|
|
childErrorToDisplay = new Error('Multiple child errors listed above ^');
|
|
|
|
// Clear the stack trace part of the stack string out because this is just a
|
|
|
|
// note about the other errors, not an actual error and is just noisy with
|
|
|
|
// that extra fluff.
|
|
|
|
childErrorToDisplay.stack = childErrorToDisplay.message;
|
|
|
|
}
|
|
|
|
|
|
|
|
const childErrorSummary = new RethrownError(
|
|
|
|
`Child process exited with code ${exitCode}${extraErrorsMessage}`,
|
|
|
|
childErrorToDisplay
|
|
|
|
);
|
|
|
|
|
|
|
|
return childErrorSummary;
|
|
|
|
}
|
|
|
|
|
2023-05-01 23:39:01 -06:00
|
|
|
async function runInChildProcess(
|
|
|
|
modulePath,
|
|
|
|
runArguments,
|
|
|
|
{ timeout, abortSignal: externalAbortSignal }
|
|
|
|
) {
|
2022-09-02 17:49:45 -06:00
|
|
|
let abortTimeoutId;
|
2022-07-05 16:30:52 -06:00
|
|
|
try {
|
|
|
|
let childErrors = [];
|
2022-09-02 17:49:45 -06:00
|
|
|
let childExitCode = '(not set yet)';
|
2023-05-01 16:33:48 -06:00
|
|
|
let childStdErr = '';
|
2022-07-05 16:30:52 -06:00
|
|
|
|
2023-05-01 23:39:01 -06:00
|
|
|
const abortController = new AbortController();
|
|
|
|
|
|
|
|
// Stops the child process if it takes too long
|
|
|
|
if (timeout) {
|
|
|
|
abortTimeoutId = setTimeout(() => {
|
|
|
|
const childErrorSummary = assembleErrorAfterChildExitsWithErrors(
|
|
|
|
childExitCode,
|
|
|
|
childErrors,
|
|
|
|
childStdErr
|
|
|
|
);
|
|
|
|
abortController.abort(
|
|
|
|
new RunInChildProcessTimeoutAbortError(
|
|
|
|
`Timed out while running ${modulePath} so we aborted the child process after ${timeout}ms. Any child errors? (${childErrors.length})`,
|
|
|
|
childErrorSummary
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}, timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop the child process if we get an external signal to stop (like if the whole
|
|
|
|
// express route that caused this call times out)
|
|
|
|
if (externalAbortSignal) {
|
|
|
|
if (externalAbortSignal.aborted) {
|
|
|
|
// Abort for good measure in case we sneak past this somehow
|
|
|
|
abortController.abort(externalAbortSignal.reason);
|
|
|
|
// Throw an error and exit early if we already aborted before we even started
|
|
|
|
throw externalAbortSignal.reason;
|
|
|
|
}
|
|
|
|
|
|
|
|
externalAbortSignal.addEventListener('abort', () => {
|
|
|
|
abortController.abort(externalAbortSignal.reason);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-09-02 19:49:06 -06:00
|
|
|
// We use a child_process because we want to be able to exit the process
|
|
|
|
// after we receive the results. We use `fork` instead of `exec`/`spawn` so
|
|
|
|
// that we can pass a module instead of running a command.
|
2023-04-24 23:50:43 -06:00
|
|
|
const child = fork(resolvedChildForkScriptPath, [modulePath], {
|
2023-05-01 23:39:01 -06:00
|
|
|
signal: abortController.signal,
|
2022-08-29 18:13:56 -06:00
|
|
|
// Default to silencing logs from the child process. We already have
|
|
|
|
// proper instrumentation of any errors that might occur.
|
|
|
|
//
|
|
|
|
// This also makes `child.stderr` and `child.stdout` available
|
|
|
|
silent: true,
|
2022-07-05 17:00:29 -06:00
|
|
|
//cwd: process.cwd(),
|
|
|
|
});
|
|
|
|
|
2022-08-29 18:13:56 -06:00
|
|
|
// Since we have to use the `silent` option for the `stderr` stuff below, we
|
2023-05-01 16:33:48 -06:00
|
|
|
// should also print out the `stdout` to our main console if we want to see what's going on.
|
|
|
|
child.stdout.on('data', function (data) {
|
|
|
|
if (logOutputFromChildProcesses) {
|
2022-08-29 18:13:56 -06:00
|
|
|
console.log('Child printed something to stdout:', String(data));
|
2023-05-01 16:33:48 -06:00
|
|
|
}
|
|
|
|
});
|
|
|
|
child.stderr.on('data', function (data) {
|
|
|
|
if (logOutputFromChildProcesses) {
|
2022-08-29 18:13:56 -06:00
|
|
|
console.log('Child printed something to stderr:', String(data));
|
2023-05-01 16:33:48 -06:00
|
|
|
}
|
|
|
|
childStdErr += data;
|
|
|
|
});
|
2022-08-29 18:13:56 -06:00
|
|
|
|
2022-09-02 19:49:06 -06:00
|
|
|
// Pass the runArguments to the child by sending instead of via argv because
|
|
|
|
// we will run into `Error: spawn E2BIG` and `Error: spawn ENAMETOOLONG`
|
|
|
|
// with argv.
|
|
|
|
child.send(runArguments);
|
2022-07-05 16:30:52 -06:00
|
|
|
|
2022-09-02 17:49:45 -06:00
|
|
|
const returnedData = await new Promise((resolve, reject) => {
|
|
|
|
let data = '';
|
2022-08-29 18:13:56 -06:00
|
|
|
// Collect the data passed back by the child
|
|
|
|
child.on('message', function (result) {
|
|
|
|
if (result.error) {
|
|
|
|
// De-serialize the error
|
|
|
|
const childError = new Error();
|
|
|
|
childError.name = result.name;
|
|
|
|
childError.message = result.message;
|
|
|
|
childError.stack = result.stack;
|
2022-09-02 19:49:06 -06:00
|
|
|
// When an error happens while running the module, we only expect one
|
|
|
|
// error to come through here from the main-line to run the module.
|
2022-08-29 18:13:56 -06:00
|
|
|
// But it's possible to get multiple errors from async out of context
|
|
|
|
// places since we also listen to `uncaughtException` and
|
|
|
|
// `unhandledRejection`.
|
|
|
|
childErrors.push(childError);
|
|
|
|
} else {
|
|
|
|
data += result.data;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-07-05 16:30:52 -06:00
|
|
|
child.on('close', (exitCode) => {
|
2022-09-02 17:49:45 -06:00
|
|
|
childExitCode = exitCode;
|
2022-07-05 16:30:52 -06:00
|
|
|
// Exited successfully
|
|
|
|
if (exitCode === 0) {
|
|
|
|
resolve(data);
|
|
|
|
} else {
|
2022-09-02 17:49:45 -06:00
|
|
|
const childErrorSummary = assembleErrorAfterChildExitsWithErrors(
|
|
|
|
childExitCode,
|
2023-05-01 16:33:48 -06:00
|
|
|
childErrors,
|
|
|
|
childStdErr
|
2022-07-05 16:30:52 -06:00
|
|
|
);
|
2022-09-02 17:49:45 -06:00
|
|
|
reject(childErrorSummary);
|
2022-07-05 16:30:52 -06:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// When a problem occurs when spawning the process or gets aborted
|
|
|
|
child.on('error', (err) => {
|
2023-05-01 23:39:01 -06:00
|
|
|
// We should be able to just `reject(err)` without any special-case handling
|
|
|
|
// here since ideally, we expect the error to be whatever `signal.reason` we
|
|
|
|
// aborted with but `child_process.fork(...)` doesn't seem play nicely, see
|
|
|
|
// https://github.com/nodejs/node/issues/47814
|
2022-07-05 16:30:52 -06:00
|
|
|
if (err.name === 'AbortError') {
|
2023-05-01 23:39:01 -06:00
|
|
|
reject(abortController.signal.reason || err);
|
2022-09-02 17:49:45 -06:00
|
|
|
} else {
|
|
|
|
reject(err);
|
2022-07-05 16:30:52 -06:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-09-02 17:49:45 -06:00
|
|
|
if (!returnedData) {
|
2023-05-01 16:33:48 -06:00
|
|
|
const childErrorSummary = assembleErrorAfterChildExitsWithErrors(
|
|
|
|
childExitCode,
|
|
|
|
childErrors,
|
|
|
|
childStdErr
|
|
|
|
);
|
2022-09-02 17:49:45 -06:00
|
|
|
throw new RethrownError(
|
2022-09-02 19:49:06 -06:00
|
|
|
`No \`returnedData\` sent from child process while running the module (${modulePath}). Any child errors? (${childErrors.length})`,
|
2022-09-02 17:49:45 -06:00
|
|
|
childErrorSummary
|
|
|
|
);
|
|
|
|
}
|
2022-07-06 18:24:29 -06:00
|
|
|
|
2022-09-02 17:49:45 -06:00
|
|
|
return returnedData;
|
|
|
|
} finally {
|
|
|
|
// We don't have to add a undefined/null check here because `clearTimeout`
|
|
|
|
// works with any value you give it and doesn't throw an error.
|
|
|
|
clearTimeout(abortTimeoutId);
|
2022-07-05 16:30:52 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-02 19:49:06 -06:00
|
|
|
module.exports = traceFunction(runInChildProcess);
|