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.
This commit is contained in:
Alex Potsides
2021-12-10 12:42:09 +00:00
committed by GitHub
parent 1b46f47fdb
commit bdc9f16d0c

View File

@ -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)
}
}
/**