Skip to content
Myr

Fix `maxListeners` warning when running multiple processes in parallel (#785)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by ehmicky · committed by GitHub · parent 4b487ce · Browse files · 3 files+116 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+116 added−2 removed

  • tests:+89 added−2 removed
  • other:+27 added−0 removed

3 files+116 added−2 removed

modified:lib/stdio/async.js+27 added
added:test/stdio/async.js+88 added
1import {once, defaultMaxListeners} from 'node:events';2import process from 'node:process';3import {setTimeout} from 'node:timers/promises';4import test from 'ava';5import {execa} from '../../index.js';6import {STANDARD_STREAMS} from '../helpers/stdio.js';7import {foobarString} from '../helpers/input.js';8import {setFixtureDir} from '../helpers/fixtures-dir.js';910setFixtureDir();1112const getStandardStreamListeners = stream => Object.fromEntries(stream.eventNames().map(eventName => [eventName, stream.listeners(eventName)]));13const getStandardStreamsListeners = () => STANDARD_STREAMS.map(stream => getStandardStreamListeners(stream));1415const getComplexStdio = isMultiple => ({16 stdin: ['pipe', 'inherit', ...(isMultiple ? [0, process.stdin] : [])],17 stdout: ['pipe', 'inherit', ...(isMultiple ? [1, process.stdout] : [])],18 stderr: ['pipe', 'inherit', ...(isMultiple ? [2, process.stderr] : [])],19});2021const testListenersCleanup = async (t, isMultiple) => {22 const streamsPreviousListeners = getStandardStreamsListeners();23 const childProcess = execa('empty.js', getComplexStdio(isMultiple));24 t.notDeepEqual(getStandardStreamsListeners(), streamsPreviousListeners);25 await Promise.all([26 childProcess,27 once(childProcess.stdin, 'unpipe'),28 once(process.stdout, 'unpipe'),29 once(process.stderr, 'unpipe'),30 ]);3132 for (const [index, streamNewListeners] of Object.entries(getStandardStreamsListeners())) {33 const defaultListeners = Object.fromEntries(Reflect.ownKeys(streamNewListeners).map(eventName => [eventName, []]));34 t.deepEqual(streamNewListeners, {...defaultListeners, ...streamsPreviousListeners[index]});35 }36};3738test.serial('process.std* listeners are cleaned up on success with a single input', testListenersCleanup, false);39test.serial('process.std* listeners are cleaned up on success with multiple inputs', testListenersCleanup, true);4041const processesCount = 100;4243test.serial('Can spawn many processes in parallel', async t => {44 const results = await Promise.all(45 Array.from({length: processesCount}, () => execa('noop.js', [foobarString])),46 );47 t.true(results.every(({stdout}) => stdout === foobarString));48});4950const testMaxListeners = async (t, isMultiple, maxListenersCount) => {51 let warning;52 const captureWarning = warningArgument => {53 warning = warningArgument;54 };5556 process.on('warning', captureWarning);5758 for (const standardStream of STANDARD_STREAMS) {59 standardStream.setMaxListeners(maxListenersCount);60 }6162 try {63 const results = await Promise.all(64 Array.from({length: processesCount}, () => execa('empty.js', getComplexStdio(isMultiple))),65 );66 await setTimeout(0);67 t.true(results.every(({exitCode}) => exitCode === 0));68 t.is(warning, undefined);69 } finally {70 for (const standardStream of STANDARD_STREAMS) {71 t.is(standardStream.getMaxListeners(), maxListenersCount);72 standardStream.setMaxListeners(defaultMaxListeners);73 }7475 process.off('warning', captureWarning);76 }77};7879test.serial('No warning with maxListeners 1 and ["pipe", "inherit"]', testMaxListeners, false, 1);80test.serial('No warning with maxListeners default and ["pipe", "inherit"]', testMaxListeners, false, defaultMaxListeners);81test.serial('No warning with maxListeners 100 and ["pipe", "inherit"]', testMaxListeners, false, 100);82test.serial('No warning with maxListeners Infinity and ["pipe", "inherit"]', testMaxListeners, false, Number.POSITIVE_INFINITY);83test.serial('No warning with maxListeners 0 and ["pipe", "inherit"]', testMaxListeners, false, 0);84test.serial('No warning with maxListeners 1 and ["pipe", "inherit"], multiple inputs', testMaxListeners, true, 1);85test.serial('No warning with maxListeners default and ["pipe", "inherit"], multiple inputs', testMaxListeners, true, defaultMaxListeners);86test.serial('No warning with maxListeners 100 and ["pipe", "inherit"], multiple inputs', testMaxListeners, true, 100);87test.serial('No warning with maxListeners Infinity and ["pipe", "inherit"], multiple inputs', testMaxListeners, true, Number.POSITIVE_INFINITY);88test.serial('No warning with maxListeners 0 and ["pipe", "inherit"], multiple inputs', testMaxListeners, true, 0);
modified:test/stream.js+1 added-2 removed
5 unmodified lines
6import getStream from 'get-stream';7import {execa, execaSync} from '../index.js';8import {setFixtureDir} from './helpers/fixtures-dir.js';9import {fullStdio, getStdio} from './helpers/stdio.js';10import {foobarString} from './helpers/input.js';1112setFixtureDir();1313const foobarString = 'foobar';1414test.serial('result.all shows both `stdout` and `stderr` intermixed', async t => {15 const {all} = await execa('noop-132.js', {all: true});16 t.is(all, '132');17});
388 unmodified lines