make deadlock feature optional

This commit is contained in:
Bryan Stitt 2023-01-15 12:12:52 -08:00
parent 7d0ac54019
commit a5df2ea00d

View File

@ -12,18 +12,22 @@ use anyhow::Context;
use futures::StreamExt; use futures::StreamExt;
use log::{debug, error, info, warn}; use log::{debug, error, info, warn};
use num::Zero; use num::Zero;
use parking_lot::deadlock;
use std::fs; use std::fs;
use std::path::Path; use std::path::Path;
use std::sync::atomic::{self, AtomicUsize}; use std::sync::atomic::{self, AtomicUsize};
use std::thread;
use tokio::runtime; use tokio::runtime;
use tokio::sync::broadcast; use tokio::sync::broadcast;
use tokio::time::Duration;
use web3_proxy::app::{flatten_handle, flatten_handles, Web3ProxyApp}; use web3_proxy::app::{flatten_handle, flatten_handles, Web3ProxyApp};
use web3_proxy::config::{CliConfig, TopConfig}; use web3_proxy::config::{CliConfig, TopConfig};
use web3_proxy::{frontend, metrics_frontend}; use web3_proxy::{frontend, metrics_frontend};
#[cfg(feature = "deadlock")]
use parking_lot::deadlock;
#[cfg(feature = "deadlock")]
use std::thread;
#[cfg(feature = "deadlock")]
use tokio::time::Duration;
fn run( fn run(
shutdown_sender: broadcast::Sender<()>, shutdown_sender: broadcast::Sender<()>,
cli_config: CliConfig, cli_config: CliConfig,
@ -34,24 +38,26 @@ fn run(
let mut shutdown_receiver = shutdown_sender.subscribe(); let mut shutdown_receiver = shutdown_sender.subscribe();
// spawn a thread for deadlock detection #[cfg(feature = "deadlock")]
// TODO: disable this feature during release mode and things should go faster {
thread::spawn(move || loop { // spawn a thread for deadlock detection
thread::sleep(Duration::from_secs(10)); thread::spawn(move || loop {
let deadlocks = deadlock::check_deadlock(); thread::sleep(Duration::from_secs(10));
if deadlocks.is_empty() { let deadlocks = deadlock::check_deadlock();
continue; if deadlocks.is_empty() {
} continue;
println!("{} deadlocks detected", deadlocks.len());
for (i, threads) in deadlocks.iter().enumerate() {
println!("Deadlock #{}", i);
for t in threads {
println!("Thread Id {:#?}", t.thread_id());
println!("{:#?}", t.backtrace());
} }
}
}); println!("{} deadlocks detected", deadlocks.len());
for (i, threads) in deadlocks.iter().enumerate() {
println!("Deadlock #{}", i);
for t in threads {
println!("Thread Id {:#?}", t.thread_id());
println!("{:#?}", t.backtrace());
}
}
});
}
// set up tokio's async runtime // set up tokio's async runtime
let mut rt_builder = runtime::Builder::new_multi_thread(); let mut rt_builder = runtime::Builder::new_multi_thread();
@ -248,6 +254,7 @@ mod tests {
}; };
use hashbrown::HashMap; use hashbrown::HashMap;
use std::env; use std::env;
use std::thread;
use web3_proxy::{ use web3_proxy::{
config::{AppConfig, Web3ConnectionConfig}, config::{AppConfig, Web3ConnectionConfig},