Skip to content

Commit 15b30da

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

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

protocols/gossipsub/src/behaviour.rs

+15-5
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 {
@@ -2079,8 +2081,11 @@ where
20792081
}
20802082
}
20812083

2082-
if let Some(m) = self.metrics.as_mut() {
2083-
m.set_topic_peers(topic_hash, peer_list.len());
2084+
if let (Some(m), Some(len)) = (
2085+
self.metrics.as_mut(),
2086+
self.topic_peers.get(topic_hash).map(|set| set.len()),
2087+
) {
2088+
m.set_topic_peers(topic_hash, len);
20842089
}
20852090
}
20862091

@@ -3337,6 +3342,11 @@ where
33373342
// support the protocol.
33383343
self.peer_topics.remove(&peer_id);
33393344

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

0 commit comments

Comments
 (0)