node/test/parallel/test-child-process-bad-stdio.js
Colin Ihrig 793c7936c3
Some checks are pending
Coverage Linux (without intl) / coverage-linux-without-intl (push) Waiting to run
Coverage Linux / coverage-linux (push) Waiting to run
Coverage Windows / coverage-windows (push) Waiting to run
Test and upload documentation to artifacts / build-docs (push) Waiting to run
Linters / lint-addon-docs (push) Waiting to run
Linters / lint-cpp (push) Waiting to run
Linters / format-cpp (push) Waiting to run
Linters / lint-js-and-md (push) Waiting to run
Linters / lint-py (push) Waiting to run
Linters / lint-yaml (push) Waiting to run
Linters / lint-sh (push) Waiting to run
Linters / lint-codeowners (push) Waiting to run
Linters / lint-pr-url (push) Waiting to run
Linters / lint-readme (push) Waiting to run
Notify on Push / Notify on Force Push on `main` (push) Waiting to run
Notify on Push / Notify on Push on `main` that lacks metadata (push) Waiting to run
Scorecard supply-chain security / Scorecard analysis (push) Waiting to run
test: fix race condition in test-child-process-bad-stdio
test-child-process-bad-stdio.js contains a race condition between
a timeout in the test process and a timeout in the spawned child
process. This commit addresses the race condition by having the
child process wait indefinitely to be killed.

PR-URL: https://github.com/nodejs/node/pull/56845
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Jake Yuesong Li <jake.yuesong@gmail.com>
2025-02-03 03:12:19 +00:00

67 lines
1.9 KiB
JavaScript

'use strict';
// Flags: --expose-internals
const common = require('../common');
if (process.argv[2] === 'child') {
setTimeout(() => {}, common.platformTimeout(1000));
return;
}
const assert = require('node:assert');
const cp = require('node:child_process');
const { mock, test } = require('node:test');
const { ChildProcess } = require('internal/child_process');
// Monkey patch spawn() to create a child process normally, but destroy the
// stdout and stderr streams. This replicates the conditions where the streams
// cannot be properly created.
const original = ChildProcess.prototype.spawn;
mock.method(ChildProcess.prototype, 'spawn', function() {
const err = original.apply(this, arguments);
this.stdout.destroy();
this.stderr.destroy();
this.stdout = null;
this.stderr = null;
return err;
});
function createChild(options, callback) {
const [cmd, opts] = common.escapePOSIXShell`"${process.execPath}" "${__filename}" child`;
options = { ...options, env: { ...opts?.env, ...options.env } };
return cp.exec(cmd, options, common.mustCall(callback));
}
test('normal execution of a child process is handled', (_, done) => {
createChild({}, (err, stdout, stderr) => {
assert.strictEqual(err, null);
assert.strictEqual(stdout, '');
assert.strictEqual(stderr, '');
done();
});
});
test('execution with an error event is handled', (_, done) => {
const error = new Error('foo');
const child = createChild({}, (err, stdout, stderr) => {
assert.strictEqual(err, error);
assert.strictEqual(stdout, '');
assert.strictEqual(stderr, '');
done();
});
child.emit('error', error);
});
test('execution with a killed process is handled', (_, done) => {
createChild({ timeout: 1 }, (err, stdout, stderr) => {
assert.strictEqual(err.killed, true);
assert.strictEqual(stdout, '');
assert.strictEqual(stderr, '');
done();
});
});