Skip to content

Commit

Permalink
fix mem-leak on topic peers
Browse files Browse the repository at this point in the history
Signed-off-by: onur-ozkan <[email protected]>
  • Loading branch information
onur-ozkan committed Dec 17, 2024
1 parent 6fc061b commit 772805a
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions protocols/gossipsub/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@

use std::{
cmp::{max, Ordering},
collections::HashSet,
collections::{hash_map::Entry, VecDeque},
collections::{BTreeSet, HashMap},
collections::{hash_map::Entry, BTreeSet, HashMap, HashSet, VecDeque},
fmt,
net::IpAddr,
task::{Context, Poll},
Expand Down Expand Up @@ -2070,6 +2068,8 @@ where

// remove topic from the peer_topics mapping
subscribed_topics.remove(topic_hash);
self.topic_peers.remove(topic_hash);

unsubscribed_peers.push((*propagation_source, topic_hash.clone()));
// generate an unsubscribe event to be polled
application_event.push(ToSwarm::GenerateEvent(Event::Unsubscribed {
Expand All @@ -2080,6 +2080,10 @@ where
}

if let Some(m) = self.metrics.as_mut() {
let peer_list = self
.topic_peers
.entry(topic_hash.clone())
.or_insert_with(Default::default);
m.set_topic_peers(topic_hash, peer_list.len());
}
}
Expand Down Expand Up @@ -3337,6 +3341,11 @@ where
// support the protocol.
self.peer_topics.remove(&peer_id);

self.topic_peers.retain(|_, peers| {
peers.remove(&peer_id);
!peers.is_empty()
});

// If metrics are enabled, register the disconnection of a peer based on its protocol.
if let Some(metrics) = self.metrics.as_mut() {
let peer_kind = &self
Expand Down

0 comments on commit 772805a

Please sign in to comment.