Skip to content

Commit 01a202a

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

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

protocols/gossipsub/src/behaviour.rs

+14-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,10 @@ where
20702068

20712069
// remove topic from the peer_topics mapping
20722070
subscribed_topics.remove(topic_hash);
2071+
if let Some(peers) = self.topic_peers.get_mut(topic_hash) {
2072+
peers.remove(propagation_source);
2073+
};
2074+
20732075
unsubscribed_peers.push((*propagation_source, topic_hash.clone()));
20742076
// generate an unsubscribe event to be polled
20752077
application_event.push(ToSwarm::GenerateEvent(Event::Unsubscribed {
@@ -2080,6 +2082,10 @@ where
20802082
}
20812083

20822084
if let Some(m) = self.metrics.as_mut() {
2085+
let peer_list = self
2086+
.topic_peers
2087+
.entry(topic_hash.clone())
2088+
.or_insert_with(Default::default);
20832089
m.set_topic_peers(topic_hash, peer_list.len());
20842090
}
20852091
}
@@ -3337,6 +3343,11 @@ where
33373343
// support the protocol.
33383344
self.peer_topics.remove(&peer_id);
33393345

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

0 commit comments

Comments
 (0)