Skip to content

Commit bdc9f16

Browse files
authored
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.
1 parent 1b46f47 commit bdc9f16

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

src/dialer/index.js

+9-7
Original file line numberDiff line numberDiff line change
@@ -155,14 +155,16 @@ class Dialer {
155155
this._pendingDialTargets.set(id, { resolve, reject })
156156
})
157157

158-
const dialTarget = await Promise.race([
159-
this._createDialTarget(peer),
160-
cancellablePromise
161-
])
162-
163-
this._pendingDialTargets.delete(id)
158+
try {
159+
const dialTarget = await Promise.race([
160+
this._createDialTarget(peer),
161+
cancellablePromise
162+
])
164163

165-
return dialTarget
164+
return dialTarget
165+
} finally {
166+
this._pendingDialTargets.delete(id)
167+
}
166168
}
167169

168170
/**

0 commit comments

Comments
 (0)