Skip to content

Commit

Permalink
fix: clean up pending dial targets (#1059)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
achingbrain authored Dec 10, 2021
1 parent 1b46f47 commit bdc9f16
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/dialer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,14 +155,16 @@ class Dialer {
this._pendingDialTargets.set(id, { resolve, reject })
})

const dialTarget = await Promise.race([
this._createDialTarget(peer),
cancellablePromise
])

this._pendingDialTargets.delete(id)
try {
const dialTarget = await Promise.race([
this._createDialTarget(peer),
cancellablePromise
])

return dialTarget
return dialTarget
} finally {
this._pendingDialTargets.delete(id)
}
}

/**
Expand Down

0 comments on commit bdc9f16

Please sign in to comment.