mirror of
https://github.com/nodejs/node.git
synced 2025-08-16 06:08:50 +02:00
worker: properly handle env and NODE_OPTIONS in workers
PR-URL: https://github.com/nodejs/node/pull/31711 Fixes: https://github.com/nodejs/node/issues/30627 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
This commit is contained in:
parent
84b8857098
commit
d63bcdd9cd
6 changed files with 193 additions and 75 deletions
|
@ -7,6 +7,7 @@ if (process.config.variables.node_without_node_options)
|
|||
|
||||
const assert = require('assert');
|
||||
const exec = require('child_process').execFile;
|
||||
const { Worker } = require('worker_threads');
|
||||
|
||||
const tmpdir = require('../common/tmpdir');
|
||||
tmpdir.refresh();
|
||||
|
@ -14,13 +15,13 @@ tmpdir.refresh();
|
|||
const printA = require.resolve('../fixtures/printA.js');
|
||||
const printSpaceA = require.resolve('../fixtures/print A.js');
|
||||
|
||||
expect(` -r ${printA} `, 'A\nB\n');
|
||||
expect(`-r ${printA}`, 'A\nB\n');
|
||||
expect(`-r ${JSON.stringify(printA)}`, 'A\nB\n');
|
||||
expect(`-r ${JSON.stringify(printSpaceA)}`, 'A\nB\n');
|
||||
expect(`-r ${printA} -r ${printA}`, 'A\nB\n');
|
||||
expect(` -r ${printA} -r ${printA}`, 'A\nB\n');
|
||||
expect(` --require ${printA} --require ${printA}`, 'A\nB\n');
|
||||
expectNoWorker(` -r ${printA} `, 'A\nB\n');
|
||||
expectNoWorker(`-r ${printA}`, 'A\nB\n');
|
||||
expectNoWorker(`-r ${JSON.stringify(printA)}`, 'A\nB\n');
|
||||
expectNoWorker(`-r ${JSON.stringify(printSpaceA)}`, 'A\nB\n');
|
||||
expectNoWorker(`-r ${printA} -r ${printA}`, 'A\nB\n');
|
||||
expectNoWorker(` -r ${printA} -r ${printA}`, 'A\nB\n');
|
||||
expectNoWorker(` --require ${printA} --require ${printA}`, 'A\nB\n');
|
||||
expect('--no-deprecation', 'B\n');
|
||||
expect('--no-warnings', 'B\n');
|
||||
expect('--no_warnings', 'B\n');
|
||||
|
@ -28,16 +29,22 @@ expect('--trace-warnings', 'B\n');
|
|||
expect('--redirect-warnings=_', 'B\n');
|
||||
expect('--trace-deprecation', 'B\n');
|
||||
expect('--trace-sync-io', 'B\n');
|
||||
expect('--trace-events-enabled', 'B\n');
|
||||
expectNoWorker('--trace-events-enabled', 'B\n');
|
||||
expect('--track-heap-objects', 'B\n');
|
||||
expect('--throw-deprecation', 'B\n');
|
||||
expect('--zero-fill-buffers', 'B\n');
|
||||
expect('--v8-pool-size=10', 'B\n');
|
||||
expect('--trace-event-categories node', 'B\n');
|
||||
expect('--throw-deprecation',
|
||||
/.*DeprecationWarning: Buffer\(\) is deprecated due to security and usability issues.*/,
|
||||
'new Buffer(42)',
|
||||
true);
|
||||
expectNoWorker('--zero-fill-buffers', 'B\n');
|
||||
expectNoWorker('--v8-pool-size=10', 'B\n');
|
||||
expectNoWorker('--trace-event-categories node', 'B\n');
|
||||
expectNoWorker(
|
||||
// eslint-disable-next-line no-template-curly-in-string
|
||||
'--trace-event-file-pattern {pid}-${rotation}.trace_events',
|
||||
'B\n'
|
||||
);
|
||||
// eslint-disable-next-line no-template-curly-in-string
|
||||
expect('--trace-event-file-pattern {pid}-${rotation}.trace_events', 'B\n');
|
||||
// eslint-disable-next-line no-template-curly-in-string
|
||||
expect('--trace-event-file-pattern {pid}-${rotation}.trace_events ' +
|
||||
expectNoWorker('--trace-event-file-pattern {pid}-${rotation}.trace_events ' +
|
||||
'--trace-event-categories node.async_hooks', 'B\n');
|
||||
expect('--unhandled-rejections=none', 'B\n');
|
||||
|
||||
|
@ -53,9 +60,9 @@ if (common.isLinux && ['arm', 'x64'].includes(process.arch)) {
|
|||
}
|
||||
|
||||
if (common.hasCrypto) {
|
||||
expect('--use-openssl-ca', 'B\n');
|
||||
expect('--use-bundled-ca', 'B\n');
|
||||
expect('--openssl-config=_ossl_cfg', 'B\n');
|
||||
expectNoWorker('--use-openssl-ca', 'B\n');
|
||||
expectNoWorker('--use-bundled-ca', 'B\n');
|
||||
expectNoWorker('--openssl-config=_ossl_cfg', 'B\n');
|
||||
}
|
||||
|
||||
// V8 options
|
||||
|
@ -63,14 +70,20 @@ expect('--abort_on-uncaught_exception', 'B\n');
|
|||
expect('--disallow-code-generation-from-strings', 'B\n');
|
||||
expect('--max-old-space-size=0', 'B\n');
|
||||
expect('--stack-trace-limit=100',
|
||||
/(\s*at f \(\[eval\]:1:\d*\)\r?\n){100}/,
|
||||
/(\s*at f \(\[(eval|worker eval)\]:1:\d*\)\r?\n)/,
|
||||
'(function f() { f(); })();',
|
||||
true);
|
||||
// Unsupported on arm. See https://crbug.com/v8/8713.
|
||||
if (!['arm', 'arm64'].includes(process.arch))
|
||||
expect('--interpreted-frames-native-stack', 'B\n');
|
||||
|
||||
function expect(opt, want, command = 'console.log("B")', wantsError = false) {
|
||||
function expectNoWorker(opt, want, command, wantsError) {
|
||||
expect(opt, want, command, wantsError, false);
|
||||
}
|
||||
|
||||
function expect(
|
||||
opt, want, command = 'console.log("B")', wantsError = false, testWorker = true
|
||||
) {
|
||||
const argv = ['-e', command];
|
||||
const opts = {
|
||||
cwd: tmpdir.path,
|
||||
|
@ -79,15 +92,52 @@ function expect(opt, want, command = 'console.log("B")', wantsError = false) {
|
|||
};
|
||||
if (typeof want === 'string')
|
||||
want = new RegExp(want);
|
||||
exec(process.execPath, argv, opts, common.mustCall((err, stdout, stderr) => {
|
||||
|
||||
const test = (type) => common.mustCall((err, stdout) => {
|
||||
const o = JSON.stringify(opt);
|
||||
if (wantsError) {
|
||||
stdout = stderr;
|
||||
assert.ok(err, `${type}: expected error for ${o}`);
|
||||
stdout = err.stack;
|
||||
} else {
|
||||
assert.ifError(err);
|
||||
assert.ifError(err, `${type}: failed for ${o}`);
|
||||
}
|
||||
if (want.test(stdout)) return;
|
||||
|
||||
const o = JSON.stringify(opt);
|
||||
assert.fail(`For ${o}, failed to find ${want} in: <\n${stdout}\n>`);
|
||||
assert.fail(
|
||||
`${type}: for ${o}, failed to find ${want} in: <\n${stdout}\n>`
|
||||
);
|
||||
});
|
||||
|
||||
exec(process.execPath, argv, opts, test('child process'));
|
||||
if (testWorker)
|
||||
workerTest(opts, command, wantsError, test('worker'));
|
||||
}
|
||||
|
||||
async function collectStream(readable) {
|
||||
readable.setEncoding('utf8');
|
||||
let data = '';
|
||||
for await (const chunk of readable) {
|
||||
data += chunk;
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
function workerTest(opts, command, wantsError, test) {
|
||||
let workerError = null;
|
||||
const worker = new Worker(command, {
|
||||
...opts,
|
||||
execArgv: [],
|
||||
eval: true,
|
||||
stdout: true,
|
||||
stderr: true
|
||||
});
|
||||
worker.on('error', (err) => {
|
||||
workerError = err;
|
||||
});
|
||||
worker.on('exit', common.mustCall((code) => {
|
||||
assert.strictEqual(code, wantsError ? 1 : 0);
|
||||
collectStream(worker.stdout).then((stdout) => {
|
||||
test(workerError, stdout);
|
||||
});
|
||||
}));
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue