Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test_runner: do not spawn run in child processes to prevent infinite loops #48849

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion lib/internal/test_runner/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ const {
convertStringToRegExp,
countCompletedTest,
kDefaultPattern,
parseCommandLine,
} = require('internal/test_runner/utils');
const { Glob } = require('internal/fs/glob');
const { once } = require('events');
Expand Down Expand Up @@ -470,13 +471,22 @@ function run(options) {
}

const root = createTestTree({ concurrency, timeout, signal });
let postRun = () => root.postRun();

// Don't spawn `run` in child processes to prevent infinite loops
const { isChildProcess, isChildProcessV8 } = parseCommandLine();
if (isChildProcess || isChildProcessV8) {
PromisePrototypeThen(PromisePrototypeThen(PromiseResolve(setup?.(root)), []), postRun);

return root.reporter;
}

let testFiles = files ?? createTestFileList();

if (shard) {
testFiles = ArrayPrototypeFilter(testFiles, (_, index) => index % shard.total === shard.index - 1);
}

let postRun = () => root.postRun();
let filesWatcher;
if (watch) {
filesWatcher = watchFiles(testFiles, root, inspectPort, signal, testNamePatterns);
Expand Down
2 changes: 2 additions & 0 deletions lib/internal/test_runner/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,8 @@ function parseCommandLine() {
testNamePatterns,
reporters,
destinations,
isChildProcess,
isChildProcessV8,
};

return globalTestOptions;
Expand Down
16 changes: 16 additions & 0 deletions test/fixtures/test-runner/run_null_files/run.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { run } from 'node:test';
import assert from 'node:assert';
import { tap } from 'node:test/reporters';

const stream = run({
timeout: 1000
})
.compose(tap)
.pipe(process.stdout)

stream.on('test:fail', (f) => {
assert.ok(false)
});
stream.on('test:pass', (p) => {
assert.ok(false)
});
22 changes: 22 additions & 0 deletions test/fixtures/test-runner/run_self_path.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { run, test } from 'node:test';
import { fileURLToPath } from 'node:url';
import assert from 'node:assert';
import { tap } from 'node:test/reporters';

const stream = run({
files: [fileURLToPath(import.meta.url)],
timeout: 1000
})
.compose(tap)
.pipe(process.stdout)

stream.on('test:fail', (f) => {
assert.ok(false)
});
stream.on('test:pass', (p) => {
assert.ok(false)
});

test('1 + 1 = 2', async () => {
assert.strictEqual(1 + 1, 2, '1 + 1 = 2')
})
18 changes: 18 additions & 0 deletions test/parallel/test-runner-run.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as fixtures from '../common/fixtures.mjs';
import { join } from 'node:path';
import { describe, it, run } from 'node:test';
import { dot, spec, tap } from 'node:test/reporters';
import { execFileSync } from 'child_process';
import assert from 'node:assert';

const testFixtures = fixtures.path('test-runner');
Expand All @@ -17,6 +18,23 @@ describe('require(\'node:test\').run', { concurrency: true }, () => {
for await (const _ of stream);
});

it('should run without infinite loops (self path)', { timeout: 10000 }, async () => {
execFileSync(
Copy link

@Mifrill Mifrill Jul 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since execFileSync is a synchronous function, it blocks the event loop until the command execution is complete and returns the result as a Buffer or a string, depending on the encoding used, hence to check execution in case of infinite loops, I think should be better to apply await explicitly:

import { promisify } from 'util';
import { execFile } from 'child_process';

const execFilePromise = promisify(execFile);
Suggested change
execFileSync(
await execFilePromise(

process.execPath,
['--test', join(testFixtures, 'run_self_path.mjs')],
{ timeout: 10000, cwd: testFixtures }
);
});

it('should run without infinite loops (Default: files)', { timeout: 10000 }, async () => {
const cwd = join(testFixtures, 'run_null_files');
execFileSync(
process.execPath,
['--test', join(cwd, 'run.mjs')],
{ timeout: 10000, cwd }
);
});

it('should fail with non existing file', async () => {
const stream = run({ files: ['a-random-file-that-does-not-exist.js'] });
stream.on('test:fail', common.mustCall(1));
Expand Down