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 18, 2024
1 parent 6fc061b commit f949f65
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 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,7 @@ where

// remove topic from the peer_topics mapping
subscribed_topics.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 @@ -2079,8 +2078,13 @@ where
}
}

let peer_list_len = peer_list.len();
if peer_list_len == 0 {
self.topic_peers.remove(topic_hash);
}

if let Some(m) = self.metrics.as_mut() {
m.set_topic_peers(topic_hash, peer_list.len());
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 f949f65

Please sign in to comment.