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

fix: call Promise.race without a long pending promise to prevent memory leak #1657

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/session-pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -997,13 +997,20 @@ export class SessionPool extends EventEmitter implements SessionPoolInterface {
throw new SessionPoolExhaustedError(this._getLeaks());
}

let removeOnceCloseListener: Function;
let removeListener: Function;

// Wait for the requested type of session to become available.
const availableEvent = type + '-available';
const promises = [
this._onClose.then(() => {
throw new GoogleError(errors.Closed);
new Promise((_, reject) => {
const onceCloseListener = () => reject(new GoogleError(errors.Closed));
this.once('close', onceCloseListener);
removeOnceCloseListener = this.removeListener.bind(
this,
'close',
onceCloseListener
);
}),
new Promise(resolve => {
this.once(availableEvent, resolve);
Expand Down Expand Up @@ -1077,6 +1084,7 @@ export class SessionPool extends EventEmitter implements SessionPoolInterface {
await Promise.race(promises);
} finally {
this._waiters[type]--;
removeOnceCloseListener!();
removeListener!();
removeErrorListener!();
removeTimeoutListener();
Expand Down