Skip to content

Commit

Permalink
Added a debug system to catch some deadlock.
Browse files Browse the repository at this point in the history
  • Loading branch information
Revertron committed Dec 3, 2023
1 parent 7091257 commit b74b0e0
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 25 deletions.
101 changes: 80 additions & 21 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ rand = { package = "rand", version = "0.8.5" }
rand-old = { package = "rand", version = "0.7.0" } # For ed25519-dalek
sqlite = "0.30.4"
uuid = { version = "1.3.0", features = ["serde", "v4"] }
mio = { version = "0.8.5", features = ["os-poll", "net"] }
mio = { version = "0.8.9", features = ["os-poll", "net"] }
ureq = { version = "2.5", optional = true }
lru = "0.9.0"
derive_more = "0.99.17"
Expand All @@ -53,10 +53,10 @@ open = { version = "3.0.3", optional = true }
[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3.9", features = ["impl-default", "wincon", "shellscalingapi"] }
windows-service = "0.6.0"
thread-priority = "0.10.0"
thread-priority = "0.13.1"

[target.'cfg(any(target_os = "linux", target_os = "freebsd", target_os = "openbsd", target_os = "netbsd"))'.dependencies]
thread-priority = "0.10.0"
thread-priority = "0.13.1"

[build-dependencies]
winres = "0.1.12"
Expand Down
28 changes: 27 additions & 1 deletion src/p2p/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::collections::{HashMap, HashSet};
use std::io::{Error, ErrorKind, Read, Write};
use std::net::{IpAddr, Shutdown, SocketAddr, SocketAddrV4, ToSocketAddrs};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex};
use std::sync::{Arc, mpsc, Mutex};
use std::time::{Duration, Instant};
use std::{io, thread};

Expand Down Expand Up @@ -84,7 +84,27 @@ impl Network {
let mut old_nodes = 0usize;
let mut old_banned = 0usize;
let mut seen_blocks = HashSet::new();

let (debug_send, debug_receive) = mpsc::channel();
let _debug_thread = thread::spawn(move || {
let mut timer = Instant::now();
let mut log = String::new();
loop {
if let Ok(line) = debug_receive.try_recv() {
timer = Instant::now();
log = line;
} else {
if timer.elapsed().as_secs() >= 60 {
timer = Instant::now();
warn!("Stuck in '{log}'");
}
thread::sleep(Duration::from_secs(1));
}
}
});

loop {
let _ = debug_send.send(String::from("Restart swarm"));
if self.peers.get_peers_count() == 0 && bootstrap_timer.elapsed().as_secs() > 60 {
warn!("Restarting swarm connections...");
wait_for_internet(WAIT_FOR_INTERNET);
Expand All @@ -93,6 +113,7 @@ impl Network {
bootstrap_timer = Instant::now();
last_events_time = Instant::now();
}
let _ = debug_send.send(String::from("Poll events"));
// Poll Mio for events, blocking until we get an event.
poll.poll(&mut events, POLL_TIMEOUT)
.unwrap_or_else(|e| warn!("Error polling sockets: {}", e));
Expand All @@ -106,6 +127,7 @@ impl Network {
// We can use the token we previously provided to `register` to determine for which socket the event is.
match event.token() {
SERVER => {
let _ = debug_send.send(String::from("Server accept"));
//debug!("Event for server socket {} is {:?}", event.token().0, &event);
// If this is an event for the server, it means a connection is ready to be accepted.
while let Ok((mut stream, mut address)) = server.accept() {
Expand Down Expand Up @@ -147,6 +169,7 @@ impl Network {
}
}
token => {
let _ = debug_send.send(String::from("Handle connection event"));
if !self.handle_connection_event(poll.registry(), event, &mut seen_blocks, &mut buffer) {
let _ = self.peers.close_peer(poll.registry(), &token);
let blocks = self.context.lock().unwrap().chain.get_height();
Expand All @@ -157,6 +180,7 @@ impl Network {
}
}
}
let _ = debug_send.send(String::from("After events iter"));
if last_events_time.elapsed().as_secs() > MAX_IDLE_SECONDS {
if self.peers.get_peers_count() > 0 {
warn!("Something is wrong with swarm connections, closing all.");
Expand All @@ -169,6 +193,7 @@ impl Network {
last_events_time = Instant::now();
}

let _ = debug_send.send(String::from("UI Timer"));
if ui_timer.elapsed().as_millis() > UI_REFRESH_DELAY_MS {
// Send pings to idle peers
let (height, max_height, hash) = {
Expand Down Expand Up @@ -210,6 +235,7 @@ impl Network {
(blocks, max_height, context.chain.get_last_hash())
};

let _ = debug_send.send(String::from("Peers update"));
let have_blocks: HashSet<u64> = self.future_blocks.values().map(|block| block.index).collect();
self.peers.update(poll.registry(), hash, height, max_height, have_blocks);
ui_timer = Instant::now();
Expand Down

0 comments on commit b74b0e0

Please sign in to comment.