Skip to content

Commit c69c677

Browse files
committed
fix: out connections leak (#3077)
1 parent 986ebd5 commit c69c677

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

waku/node/peer_manager/peer_manager.nim

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -399,17 +399,19 @@ proc onPeerMetadata(pm: PeerManager, peerId: PeerId) {.async.} =
399399
asyncSpawn(pm.switch.disconnect(peerId))
400400
pm.peerStore.delete(peerId)
401401

402-
proc connectedPeers*(pm: PeerManager, protocol: string): (seq[PeerId], seq[PeerId]) =
403-
## Returns the peerIds of physical connections (in and out)
404-
## containing at least one stream with the given protocol.
402+
proc connectedPeers*(
403+
pm: PeerManager, protocol: string = ""
404+
): (seq[PeerId], seq[PeerId]) =
405+
## Returns the peerIds of physical connections (in and out)
406+
## If a protocol is specified, only returns peers with at least one stream of that protocol
405407

406408
var inPeers: seq[PeerId]
407409
var outPeers: seq[PeerId]
408410

409411
for peerId, muxers in pm.switch.connManager.getConnections():
410412
for peerConn in muxers:
411413
let streams = peerConn.getStreams()
412-
if streams.anyIt(it.protocol == protocol):
414+
if protocol.len == 0 or streams.anyIt(it.protocol == protocol):
413415
if peerConn.connection.transportDir == Direction.In:
414416
inPeers.add(peerId)
415417
elif peerConn.connection.transportDir == Direction.Out:

waku/node/waku_node.nim

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1239,16 +1239,19 @@ proc mountLibp2pPing*(node: WakuNode) {.async: (raises: []).} =
12391239
# TODO: Move this logic to PeerManager
12401240
proc keepaliveLoop(node: WakuNode, keepalive: chronos.Duration) {.async.} =
12411241
while node.started:
1242-
# Keep all connected peers alive while running
1242+
# Keep connected peers alive while running
1243+
# Each node is responsible of keeping its outgoing connections alive
12431244
trace "Running keepalive"
12441245

12451246
# First get a list of connected peer infos
1246-
let peers =
1247-
node.peerManager.peerStore.peers().filterIt(it.connectedness == Connected)
1247+
let outPeers = node.peerManager.connectedPeers()[1]
12481248

1249-
for peer in peers:
1249+
for peerId in outPeers:
12501250
try:
1251-
let conn = await node.switch.dial(peer.peerId, peer.addrs, PingCodec)
1251+
info "calling keepAlive dial", peerId = peerId
1252+
let conn = (await node.peerManager.dialPeer(peerId, PingCodec)).valueOr:
1253+
warn "Failed dialing peer for keep alive", peerId = peerId
1254+
continue
12521255
let pingDelay = await node.libp2pPing.ping(conn)
12531256
await conn.close()
12541257
except CatchableError as exc:

0 commit comments

Comments
 (0)