Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid maximum listeners exceeded warning #4502

Merged
merged 2 commits into from May 19, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/utils/hookActions.ts
@@ -1,3 +1,4 @@
import { EventEmitter } from 'events';
import process from 'process';
import { HookAction, PluginDriver } from './PluginDriver';

Expand All @@ -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<T>(
pluginDriver: PluginDriver,
callback: () => Promise<T>
Expand All @@ -34,10 +42,10 @@ export async function catchUnfinishedHookActions<T>(
)
);
};
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;
}