From 0537d932c38d9e441c38812c89869a385789bf90 Mon Sep 17 00:00:00 2001 From: Lukas Taegert-Atkinson Date: Thu, 19 May 2022 06:45:01 +0200 Subject: [PATCH] Avoid maximum listeners exceeded warning (#4502) --- src/utils/hookActions.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/utils/hookActions.ts b/src/utils/hookActions.ts index 69269bbb27f..01608209f55 100644 --- a/src/utils/hookActions.ts +++ b/src/utils/hookActions.ts @@ -1,3 +1,4 @@ +import { EventEmitter } from 'events'; import process from 'process'; import { HookAction, PluginDriver } from './PluginDriver'; @@ -19,6 +20,13 @@ function formatAction([pluginName, hookName, args]: HookAction): string { return action; } +// We do not directly listen on process to avoid max listeners warnings for +// complicated build processes +const beforeExitEvent = 'beforeExit'; +const beforeExitEmitter = new EventEmitter(); +beforeExitEmitter.setMaxListeners(0); +process.on(beforeExitEvent, () => beforeExitEmitter.emit(beforeExitEvent)); + export async function catchUnfinishedHookActions( pluginDriver: PluginDriver, callback: () => Promise @@ -34,10 +42,10 @@ export async function catchUnfinishedHookActions( ) ); }; - process.once('beforeExit', handleEmptyEventLoop); + beforeExitEmitter.once(beforeExitEvent, handleEmptyEventLoop); }); const result = await Promise.race([callback(), emptyEventLoopPromise]); - process.off('beforeExit', handleEmptyEventLoop!); + beforeExitEmitter.off(beforeExitEvent, handleEmptyEventLoop!); return result; }