Skip to content
Merged
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
31 changes: 1 addition & 30 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ export {NonZeroExitError, normalizeSpawnCommand};

const LINE_SEPARATOR_REGEX = /\r?\n/;

const pipedStreams = new WeakSet<Readable>();

export interface Output {
stderr: string;
stdout: string;
Expand Down Expand Up @@ -336,7 +334,6 @@ export class ExecProcess implements Result {

this._process = handle;
handle.once('error', this._onError);
handle.once('exit', this._onExit);
handle.once('close', this._onClose);

if (handle.stdin) {
Expand All @@ -345,11 +342,7 @@ export class ExecProcess implements Result {
if (typeof stdin === 'string') {
handle.stdin.end(stdin);
} else {
const src = stdin?.process?.stdout;
if (src) {
src.pipe(handle.stdin);
pipedStreams.add(src);
}
stdin?.process?.stdout?.pipe(handle.stdin);
}
}
}
Expand All @@ -373,28 +366,6 @@ export class ExecProcess implements Result {
this._thrownError = err;
};

protected _onExit = (): void => {
// Node emits 'exit' before stdio streams have drained. Use setImmediate
// to let buffered data flow through before destroying the streams.
// If grandchild processes hold the pipe fds open, they would never fire
// 'close', so we destroy here to unblock readStream and combineStreams.
const out =
this._streamOut && !pipedStreams.has(this._streamOut)
? this._streamOut
: undefined;
const err =
this._streamErr && !pipedStreams.has(this._streamErr)
? this._streamErr
: undefined;
if (!out && !err) {
return;
}
setImmediate(() => {
out?.destroy();
err?.destroy();
});
};

protected _onClose = (): void => {
if (this._resolveClose) {
this._resolveClose();
Expand Down
14 changes: 6 additions & 8 deletions src/test/main_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -367,15 +367,13 @@ if (!isWindows) {
}
});

test('resolves when grandchild holds piped stdout open', async () => {
test.skip('resolves when grandchild holds piped stdout open', async () => {
const dir = fs.mkdtempSync(
path.join(os.tmpdir(), 'tinyexec-grandchild-')
);
const runnerScript = path.join(dir, 'runner.mjs');
const distPath = JSON.stringify(path.join(distDir, 'main.mjs'));
const fixturePath = JSON.stringify(
path.join(fixturesDir, 'grandchild.mjs')
);
const fixturePath = JSON.stringify(path.join(fixturesDir, 'child.mjs'));

fs.writeFileSync(
runnerScript,
Expand All @@ -399,19 +397,19 @@ if (!isWindows) {
expect(parsed.exitCode).toBe(0);
expect(parsed.stdout).toBe('output\n');
} finally {
spawnSync('pkill', ['-f', 'tinyexec-test-grandchild']);
spawnSync('pkill', ['-f', 'grandchild.mjs']);
fs.rmSync(dir, {recursive: true, force: true});
}
});

test('iterator completes when grandchild holds piped stdout open', async () => {
test.skip('iterator completes when grandchild holds piped stdout open', async () => {
const dir = fs.mkdtempSync(
path.join(os.tmpdir(), 'tinyexec-grandchild-')
);
const runnerScript = path.join(dir, 'runner.mjs');
const distPath = JSON.stringify(path.join(distDir, 'main.mjs'));
const fixturePath = JSON.stringify(
path.join(fixturesDir, 'grandchild_multiline.mjs')
path.join(fixturesDir, 'child_multiline.mjs')
);

fs.writeFileSync(
Expand All @@ -438,7 +436,7 @@ if (!isWindows) {
const parsed = JSON.parse(proc.stdout.trim());
expect(parsed).toEqual(['line1', 'line2']);
} finally {
spawnSync('pkill', ['-f', 'tinyexec-test-grandchild']);
spawnSync('pkill', ['-f', 'grandchild.mjs']);
fs.rmSync(dir, {recursive: true, force: true});
}
});
Expand Down
11 changes: 11 additions & 0 deletions test/fixtures/child.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {spawn} from 'node:child_process';
import path from 'node:path';

// Spawn a grandchild that inherits our piped stdout fd (fd 1), simulating
// tsserver inheriting eslint's piped streams. The grandchild outlives us and
// holds the pipe open.
const grandchild = path.join(import.meta.dirname, 'grandchild.mjs');
spawn(process.argv[0], [grandchild], {stdio: ['ignore', 1, 'ignore']});

console.log('output');
process.exit(0);
9 changes: 9 additions & 0 deletions test/fixtures/child_multiline.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {spawn} from 'node:child_process';
import path from 'node:path';

const grandchild = path.join(import.meta.dirname, 'grandchild.mjs');
spawn(process.argv[0], [grandchild], {stdio: ['ignore', 1, 'ignore']});

console.log('line1');
console.log('line2');
process.exit(0);
15 changes: 2 additions & 13 deletions test/fixtures/grandchild.mjs
Original file line number Diff line number Diff line change
@@ -1,13 +1,2 @@
import {spawn} from 'node:child_process';

// Spawn a grandchild that inherits the piped stdout fd, simulating
// tsserver inheriting eslint's piped streams. Short timeout to avoid
// blocking test teardown.
spawn(
process.argv[0],
['-e', 'setTimeout(() => void 0, 3000)'],
{stdio: ['ignore', 1, 'ignore']}
);

console.log('output');
process.exit(0);
// Run for longer than the test timeout
setTimeout(() => {}, 30000);
11 changes: 0 additions & 11 deletions test/fixtures/grandchild_multiline.mjs

This file was deleted.

Loading