Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: prevent duplicate connections to multiaddr #2734

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix: prevent duplicate connections to multiaddr
After opening a new connection, check if there is an existing connection
to the same peer. If the existing connection is not limited or the new
connection is also limit, return the existing connection.

This will prevent multiple connections to the same peer in cases where
the multiaddr being dialed does not contain a peer id.
tabcat committed Sep 29, 2024
commit 91563833e93870f483f344fbc3439dd747c77cd4
27 changes: 26 additions & 1 deletion packages/libp2p/src/connection-manager/dial-queue.ts
Original file line number Diff line number Diff line change
@@ -135,11 +135,12 @@
*/
async dial (peerIdOrMultiaddr: PeerId | Multiaddr | Multiaddr[], options: OpenConnectionOptions = {}): Promise<Connection> {
const { peerId, multiaddrs } = getPeerAddress(peerIdOrMultiaddr)
const { force } = options

// make sure we don't have an existing connection to any of the addresses we
// are about to dial
const existingConnection = Array.from(this.connections.values()).flat().find(conn => {
if (options.force === true) {
if (force === true) {
return false
}

@@ -260,6 +261,30 @@
this.log.error('could not update last dial failure key for %p', peerId, err)
}

const { remotePeer } = conn

// make sure we don't have an existing connection to the address we dialed
const existingConnection = Array.from(this.connections.values()).flat().find(_conn => {
if (force === true) {
return false
}

Check warning on line 270 in packages/libp2p/src/connection-manager/dial-queue.ts

Codecov / codecov/patch

packages/libp2p/src/connection-manager/dial-queue.ts#L269-L270

Added lines #L269 - L270 were not covered by tests

if (_conn.remotePeer.equals(remotePeer) && _conn !== conn) {
return true
}

Check warning on line 274 in packages/libp2p/src/connection-manager/dial-queue.ts

Codecov / codecov/patch

packages/libp2p/src/connection-manager/dial-queue.ts#L273-L274

Added lines #L273 - L274 were not covered by tests

return false
})

// return existing, open connection to peer if equal or better limits
if (existingConnection?.status === 'open' && (existingConnection?.limits == null || conn?.limits != null)) {
this.log('already connected to %a', existingConnection.remoteAddr)
options?.onProgress?.(new CustomProgressEvent('dial-queue:already-connected'))
this.log('closing duplicate connection to %p', remotePeer)
await conn.close()
return existingConnection

Check warning on line 285 in packages/libp2p/src/connection-manager/dial-queue.ts

Codecov / codecov/patch

packages/libp2p/src/connection-manager/dial-queue.ts#L281-L285

Added lines #L281 - L285 were not covered by tests
}

return conn
} catch (err: any) {
this.log.error('dial failed to %a', address.multiaddr, err)