Skip to content

Fix the duplicate connection bug #499

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 43 additions & 21 deletions network/src/p2p/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,7 @@ impl IoHandler<Message> for Handler {
is_inbound: true,
} => {
let mut inbound_connections = self.inbound_connections.write();
let outbound_connections = self.outbound_connections.write();
let target = connection.peer_addr();
self.peer_db.insert(*target);
if let Some(token) = self.inbound_tokens.lock().gen() {
Expand All @@ -517,17 +518,26 @@ impl IoHandler<Message> for Handler {
remote_node_id,
token
);
assert_eq!(
None,
self.remote_node_ids_reverse.write().insert(remote_node_id, token),
"{}:{} is already registered",
remote_node_id,
token
);

let t = inbound_connections.insert(token, connection);
assert!(t.is_none());
io.register_stream(token);
if !self.remote_node_ids_reverse.write().contains_key(&remote_node_id) {
assert_eq!(
None,
self.remote_node_ids_reverse.write().insert(remote_node_id, token),
"{}:{} is already registered",
remote_node_id,
token
);
}
let can_insert = inbound_connections
.values()
.all(|in_connection| in_connection.peer_addr() != connection.peer_addr());
let is_not_out = outbound_connections
.values()
.all(|out_connection| out_connection.peer_addr() != connection.peer_addr());
if can_insert && is_not_out {
let t = inbound_connections.insert(token, connection);
assert!(t.is_none());
io.register_stream(token);
}
} else {
cwarn!(NETWORK, "Cannot establish an inbound connection");
}
Expand All @@ -537,6 +547,7 @@ impl IoHandler<Message> for Handler {
is_inbound: false,
} => {
let mut outbound_connections = self.outbound_connections.write();
let inbound_connections = self.inbound_connections.write();
if let Some(token) = self.outbound_tokens.lock().gen() {
let peer_addr = *connection.peer_addr();
let remote_node_id = peer_addr.into();
Expand All @@ -547,13 +558,15 @@ impl IoHandler<Message> for Handler {
remote_node_id,
token
);
assert_eq!(
None,
self.remote_node_ids_reverse.write().insert(remote_node_id, token),
"{}:{} is already registered",
remote_node_id,
token
);
if !self.remote_node_ids_reverse.write().contains_key(&remote_node_id) {
assert_eq!(
None,
self.remote_node_ids_reverse.write().insert(remote_node_id, token),
"{}:{} is already registered",
remote_node_id,
token
);
}

let mut network_message_size = 0;
for (name, versions) in self.client.extension_versions() {
Expand All @@ -566,9 +579,18 @@ impl IoHandler<Message> for Handler {
network_message_size,
);
}
let t = outbound_connections.insert(token, connection);
assert!(t.is_none());
io.register_stream(token);
let can_insert = outbound_connections
.values()
.all(|out_connection| out_connection.peer_addr() == connection.peer_addr());
let is_not_in = inbound_connections
.values()
.all(|in_connection| in_connection.peer_addr() == connection.peer_addr());

if can_insert && is_not_in {
let t = outbound_connections.insert(token, connection);
assert!(t.is_none());
io.register_stream(token);
}
} else {
cwarn!(NETWORK, "Cannot establish an outbound connection");
}
Expand Down
23 changes: 14 additions & 9 deletions sync/src/block/extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -476,22 +476,27 @@ impl NetworkExtension<Event> for Extension {
cinfo!(SYNC, "New peer detected #{}", id);
self.send_status(id);

let t = self.connected_nodes.insert(*id);
debug_assert!(t, "{} is already added to peer list", id);
if !self.connected_nodes.contains(id) {
Copy link
Contributor

@majecty majecty Sep 7, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MSNTCS
It seems this patch is avoiding the error case. Not fixing the error.
When the on_node_added is called, it should not be included in connected_nodes.
Could you try to fix this problem fundamentally?

let t = self.connected_nodes.insert(*id);
debug_assert!(t, "{} is already added to peer list", id);
}

let token = self.token_generator.gen().expect("Token generator is full");
let token_info = TokenInfo {
node_id: *id,
request_id: None,
};

let t = self.requests.insert(*id, Vec::new());
debug_assert_eq!(None, t);
let t = self.tokens_info.insert(token, token_info);
debug_assert_eq!(None, t);
let t = self.tokens.insert(*id, token);
debug_assert_eq!(None, t);
debug_assert!(t.is_none());
if !self.requests.contains_key(id) {
let t = self.requests.insert(*id, Vec::new());
debug_assert_eq!(None, t);
}
self.tokens_info.entry(token).or_insert(token_info);
if !self.tokens.contains_key(id) {
let t = self.tokens.insert(*id, token);
debug_assert_eq!(None, t);
debug_assert!(t.is_none());
}
}

fn on_node_removed(&mut self, id: &NodeId) {
Expand Down