-
-
Notifications
You must be signed in to change notification settings - Fork 820
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: avoid "possible EventEmitter memory leak detected" warning (#6789)
* fix: possible EventEmitter memory leak detected * Single place for AbortSignal listeners -> #6789 (#6793) * Single place for AbortSignal listeners * Fix * .. * Lets go * Small improvement * Changeset for utils * Update packages/executor/src/execution/__tests__/abort-signal.test.ts --------- Co-authored-by: Arda TANRIKULU <ardatanrikulu@gmail.com>
- Loading branch information
Showing
7 changed files
with
122 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
'@graphql-tools/utils': minor | ||
--- | ||
|
||
- New helper function `getAbortPromise` to get a promise rejected when `AbortSignal` is aborted | ||
- New helper function `registerAbortSignalListener` to register a listener to abort a promise when `AbortSignal` is aborted | ||
|
||
Instead of using `.addEventListener('abort', () => {/* ... */})`, we register a single listener to avoid warnings on Node.js like `MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 abort listeners added. Use emitter.setMaxListeners() to increase limit`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
'@graphql-tools/executor': patch | ||
--- | ||
|
||
Surpress the "possible EventEmitter memory leak detected." warning occuring on Node.js when passing | ||
a `AbortSignal` to `execute`. | ||
|
||
Each execution will now only set up a single listener on the supplied `AbortSignal`. While the warning is harmless it can be misleading, which is the main motivation of this change. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { memoize1 } from './memoize.js'; | ||
|
||
// AbortSignal handler cache to avoid the "possible EventEmitter memory leak detected" | ||
// on Node.js | ||
const getListenersOfAbortSignal = memoize1(function getListenersOfAbortSignal(signal: AbortSignal) { | ||
const listeners = new Set<EventListener>(); | ||
signal.addEventListener( | ||
'abort', | ||
e => { | ||
for (const listener of listeners) { | ||
listener(e); | ||
} | ||
}, | ||
{ once: true }, | ||
); | ||
return listeners; | ||
}); | ||
|
||
/** | ||
* Register an AbortSignal handler for a signal. | ||
* This helper function mainly exists to work around the | ||
* "possible EventEmitter memory leak detected. 11 listeners added. Use emitter.setMaxListeners() to increase limit." | ||
* warning occuring on Node.js | ||
*/ | ||
export function registerAbortSignalListener(signal: AbortSignal, listener: VoidFunction) { | ||
// If the signal is already aborted, call the listener immediately | ||
if (signal.aborted) { | ||
listener(); | ||
return; | ||
} | ||
getListenersOfAbortSignal(signal).add(listener); | ||
} | ||
|
||
export const getAbortPromise = memoize1(function getAbortPromise(signal: AbortSignal) { | ||
return new Promise<void>((_resolve, reject) => { | ||
// If the signal is already aborted, return a rejected promise | ||
if (signal.aborted) { | ||
reject(signal.reason); | ||
return; | ||
} | ||
registerAbortSignalListener(signal, () => { | ||
reject(signal.reason); | ||
}); | ||
}); | ||
}); |