Skip to content

Commit e90c0f5

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

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

protocols/gossipsub/src/behaviour.rs

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

20712069
// remove topic from the peer_topics mapping
20722070
subscribed_topics.remove(topic_hash);
2071+
self.topic_peers.retain(|th, peers| {
2072+
if th == topic_hash {
2073+
peers.remove(propagation_source);
2074+
}
2075+
2076+
!peers.is_empty()
2077+
});
2078+
20732079
unsubscribed_peers.push((*propagation_source, topic_hash.clone()));
20742080
// generate an unsubscribe event to be polled
20752081
application_event.push(ToSwarm::GenerateEvent(Event::Unsubscribed {
@@ -2080,6 +2086,10 @@ where
20802086
}
20812087

20822088
if let Some(m) = self.metrics.as_mut() {
2089+
let peer_list = self
2090+
.topic_peers
2091+
.entry(topic_hash.clone())
2092+
.or_insert_with(Default::default);
20832093
m.set_topic_peers(topic_hash, peer_list.len());
20842094
}
20852095
}
@@ -3337,6 +3347,11 @@ where
33373347
// support the protocol.
33383348
self.peer_topics.remove(&peer_id);
33393349

3350+
self.topic_peers.retain(|_, peers| {
3351+
peers.remove(&peer_id);
3352+
!peers.is_empty()
3353+
});
3354+
33403355
// If metrics are enabled, register the disconnection of a peer based on its protocol.
33413356
if let Some(metrics) = self.metrics.as_mut() {
33423357
let peer_kind = &self

0 commit comments

Comments
 (0)