Skip to content

Commit

Permalink
tools: Fix EventEmitter leak in await-packagist-updates.mjs
Browse files Browse the repository at this point in the history
Each time through the loop we're adding a listener on the http2Client
and never removing it. Fix that.
  • Loading branch information
anomiex committed Jul 24, 2024
1 parent f894b03 commit f311c69
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions tools/js-tools/await-packagist-updates.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ function pollPackagist( name, versionRange ) {
task: async ( ctx, task ) => {
while ( ! aborter.aborted ) {
try {
let req;
const cleanup = [];
// http2 isn't a promise-based API, so wrap in a promise ourselves and await it.
const done = await new Promise( ( resolve, reject ) => {
// Reject the promise with a fatal-flagged error.
Expand Down Expand Up @@ -112,9 +112,22 @@ function pollPackagist( name, versionRange ) {
// So listen for that just in case, because hung promises are bad.
// @see https://nodejs.org/api/http2.html#error-handling
http2Client.on( 'error', reject );
cleanup.push( () => {
if ( http2Client ) {
http2Client.off( 'error', reject );
}
} );

// Make the actual request.
req = http2Client.request( reqHeaders, { signal: aborter.signal } );
const req = http2Client.request( reqHeaders, { signal: aborter.signal } );
cleanup.push( () => {
// Make sure the request actually gets closed whenever the promise settles.
// We don't want hung connections waiting on us to read more data or something.
if ( ! req.closed && ! req.destroyed ) {
req.close( http2.constants.NGHTTP2_CANCEL );
}
} );

req.on( 'error', reject );
req.on( 'response', resHeaders => {
const status = resHeaders[ http2.constants.HTTP2_HEADER_STATUS ];
Expand Down Expand Up @@ -178,10 +191,8 @@ function pollPackagist( name, versionRange ) {
} );
req.end();
} ).finally( () => {
// Make sure the request actually gets closed whenever the promise settles.
// We don't want hung connections waiting on us to read more data or something.
if ( req && ! req.closed && ! req.destroyed ) {
req.close( http2.constants.NGHTTP2_CANCEL );
for ( const cb of cleanup ) {
cb();
}
} );

Expand Down

0 comments on commit f311c69

Please sign in to comment.