Skip to content

Commit

Permalink
fix: abort dial targets and creation of other connections to avoid me…
Browse files Browse the repository at this point in the history
…mory leaks (libp2p#1616)
  • Loading branch information
maschad committed Mar 29, 2023
1 parent 7599911 commit faa5314
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/connection-manager/dialer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,15 @@ export class DefaultDialer implements Startable, Dialer {

if (Array.isArray(multiaddr)) {
// Return the first successful connection
return (await Promise.all(multiaddr.map(async (addr) => await this.createDialTarget(peerId, addr, options))))[0]
const connection = (await Promise.all(multiaddr.map(async (addr) => await this.createDialTarget(peerId, addr, options))))[0]

// abort the creation of any other connections and/or close them, otherwise this will leak memory and sockets.
this.getPendingDialTargets().forEach((controller, id) => {
controller.abort()
this.pendingDialTargets.delete(id)
})

return connection
} else {
return await this.createDialTarget(peerId, multiaddr, options)
}
Expand Down

0 comments on commit faa5314

Please sign in to comment.