Skip to content

Commit 6be7ea2

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

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

protocols/gossipsub/src/behaviour.rs

+15-3
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,11 @@ where
20702068

20712069
// remove topic from the peer_topics mapping
20722070
subscribed_topics.remove(topic_hash);
2071+
self.topic_peers.retain(|_, peers| {
2072+
peers.remove(propagation_source);
2073+
!peers.is_empty()
2074+
});
2075+
20732076
unsubscribed_peers.push((*propagation_source, topic_hash.clone()));
20742077
// generate an unsubscribe event to be polled
20752078
application_event.push(ToSwarm::GenerateEvent(Event::Unsubscribed {
@@ -2080,6 +2083,10 @@ where
20802083
}
20812084

20822085
if let Some(m) = self.metrics.as_mut() {
2086+
let peer_list = self
2087+
.topic_peers
2088+
.entry(topic_hash.clone())
2089+
.or_insert_with(Default::default);
20832090
m.set_topic_peers(topic_hash, peer_list.len());
20842091
}
20852092
}
@@ -3337,6 +3344,11 @@ where
33373344
// support the protocol.
33383345
self.peer_topics.remove(&peer_id);
33393346

3347+
self.topic_peers.retain(|_, peers| {
3348+
peers.remove(&peer_id);
3349+
!peers.is_empty()
3350+
});
3351+
33403352
// If metrics are enabled, register the disconnection of a peer based on its protocol.
33413353
if let Some(metrics) = self.metrics.as_mut() {
33423354
let peer_kind = &self

0 commit comments

Comments
 (0)