From bdc9f16d0cbe56ccf26822f11068e7795bcef046 Mon Sep 17 00:00:00 2001 From: Alex Potsides Date: Fri, 10 Dec 2021 12:42:09 +0000 Subject: [PATCH] fix: clean up pending dial targets (#1059) If the `Promise.race` throws, execution of the function is terminated so the pending dial target is never removed from the map and we leak memory. This can happen when there are invalid multiaddrs or when a peer reports more dialable addresses than the threshold. Instead wrap the `Promise.race` in a `try/finally` which will always remove the pending dial target in the event of success or failure. --- src/dialer/index.js | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/dialer/index.js b/src/dialer/index.js index 0cd5e088..b4dd87c2 100644 --- a/src/dialer/index.js +++ b/src/dialer/index.js @@ -155,14 +155,16 @@ class Dialer { this._pendingDialTargets.set(id, { resolve, reject }) }) - const dialTarget = await Promise.race([ - this._createDialTarget(peer), - cancellablePromise - ]) + try { + const dialTarget = await Promise.race([ + this._createDialTarget(peer), + cancellablePromise + ]) - this._pendingDialTargets.delete(id) - - return dialTarget + return dialTarget + } finally { + this._pendingDialTargets.delete(id) + } } /**