From 593c2bcb5502ace0a10f23eff4f7d6ebaef8f483 Mon Sep 17 00:00:00 2001 From: Marcin Szczepanski Date: Wed, 14 Dec 2022 11:32:34 +1100 Subject: [PATCH] Ensure maxListeners for process.stdout accounts for workers (#8689) --- packages/core/workers/src/WorkerFarm.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/packages/core/workers/src/WorkerFarm.js b/packages/core/workers/src/WorkerFarm.js index afeb0717e61..a5d6a3dbf0b 100644 --- a/packages/core/workers/src/WorkerFarm.js +++ b/packages/core/workers/src/WorkerFarm.js @@ -101,6 +101,21 @@ export default class WorkerFarm extends EventEmitter { this.localWorker.childInit != null ? this.localWorker.childInit() : null; this.run = this.createHandle('run'); + // Worker thread stdout is by default piped into the process stdout, if there are enough worker + // threads to exceed the default listener limit, then anything else piping into stdout will trigger + // the `MaxListenersExceededWarning`, so we should ensure the max listeners is at least equal to the + // number of workers + 1 for the main thread. + // + // Note this can't be fixed easily where other things pipe into stdout - even after starting > 10 worker + // threads `process.stdout.getMaxListeners()` will still return 10, however adding another pipe into `stdout` + // will give the warning with `` as the number of listeners. + process.stdout.setMaxListeners( + Math.max( + process.stdout.getMaxListeners(), + WorkerFarm.getNumWorkers() + 1, + ), + ); + this.startMaxWorkers(); }