Skip to content

Commit f949f65

Browse files
committed
fix mem-leak on topic peers
Signed-off-by: onur-ozkan <[email protected]>
1 parent 6fc061b commit f949f65

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

protocols/gossipsub/src/behaviour.rs

+13-4
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@
2020

2121
use std::{
2222
cmp::{max, Ordering},
23-
collections::HashSet,
24-
collections::{hash_map::Entry, VecDeque},
25-
collections::{BTreeSet, HashMap},
23+
collections::{hash_map::Entry, BTreeSet, HashMap, HashSet, VecDeque},
2624
fmt,
2725
net::IpAddr,
2826
task::{Context, Poll},
@@ -2070,6 +2068,7 @@ where
20702068

20712069
// remove topic from the peer_topics mapping
20722070
subscribed_topics.remove(topic_hash);
2071+
20732072
unsubscribed_peers.push((*propagation_source, topic_hash.clone()));
20742073
// generate an unsubscribe event to be polled
20752074
application_event.push(ToSwarm::GenerateEvent(Event::Unsubscribed {
@@ -2079,8 +2078,13 @@ where
20792078
}
20802079
}
20812080

2081+
let peer_list_len = peer_list.len();
2082+
if peer_list_len == 0 {
2083+
self.topic_peers.remove(topic_hash);
2084+
}
2085+
20822086
if let Some(m) = self.metrics.as_mut() {
2083-
m.set_topic_peers(topic_hash, peer_list.len());
2087+
m.set_topic_peers(topic_hash, peer_list_len);
20842088
}
20852089
}
20862090

@@ -3337,6 +3341,11 @@ where
33373341
// support the protocol.
33383342
self.peer_topics.remove(&peer_id);
33393343

3344+
self.topic_peers.retain(|_, peers| {
3345+
peers.remove(&peer_id);
3346+
!peers.is_empty()
3347+
});
3348+
33403349
// If metrics are enabled, register the disconnection of a peer based on its protocol.
33413350
if let Some(metrics) = self.metrics.as_mut() {
33423351
let peer_kind = &self

0 commit comments

Comments
 (0)