From 1cb0db4cbef4be0d6feae6a30f33b8dc75b49a33 Mon Sep 17 00:00:00 2001 From: Marcin Szczepanski Date: Tue, 13 Dec 2022 14:40:53 +1100 Subject: [PATCH] Ensure maxListeners for process.stdout accounts for workers --- 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(); }