diff --git a/src/main.ts b/src/main.ts index aa9dc0c..89839bc 100644 --- a/src/main.ts +++ b/src/main.ts @@ -350,6 +350,15 @@ export class ExecProcess implements Result { if (handle.stdin) { const {stdin} = options; + // A child may exit without reading all of its input (e.g. `head`), + // which surfaces as EPIPE here (EOF on Windows). That is not a failure + // of ours. + handle.stdin.on('error', (err: NodeJS.ErrnoException) => { + if (err.code !== 'EPIPE' && err.code !== 'EOF') { + this._thrownError = err; + } + }); + if (typeof stdin === 'string') { handle.stdin.end(stdin); } else { diff --git a/src/test/main_test.ts b/src/test/main_test.ts index a852620..7b48f5d 100644 --- a/src/test/main_test.ts +++ b/src/test/main_test.ts @@ -194,6 +194,15 @@ describe('exec (async)', () => { expect(result.exitCode).toBe(0); }); + test('does not crash when the process exits without reading stdin', async () => { + // Larger than the pipe buffer, so the write is still pending on exit + const result = await x('node', ['-e', 'process.exit(0)'], { + stdin: 'x'.repeat(4 * 1024 * 1024) + }); + + expect(result.exitCode).toBe(0); + }); + test('supports stdin passed as another process (Result)', async () => { const proc = x('node', ['-e', "process.stdout.write('foo\\nbar')"]); const result = await x(