Skip to content
Open
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
22 changes: 15 additions & 7 deletions src/bin/electrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,17 +122,25 @@ fn run_server(config: Arc<Config>) -> Result<()> {
if let Err(err) = signal.wait(Duration::from_millis(config.main_loop_delay), true) {
info!("stopping server: {}", err);

electrs::util::spawn_thread("shutdown-thread-checker", || {
let mut counter = 40;
let interval_ms = 500;

while counter > 0 {
// Watchdog: give the graceful shutdown ~5 seconds to close all TCP
// connections. If it hasn't completed by then (e.g. clients holding
// sockets open with slow/zombie connections), force the process to
// exit so we don't hang indefinitely.
electrs::util::spawn_thread("shutdown-watchdog", || {
let timeout = Duration::from_secs(5);
let interval = Duration::from_millis(500);
let mut elapsed = Duration::ZERO;

while elapsed < timeout {
electrs::util::with_spawned_threads(|threads| {
debug!("Threads during shutdown: {:?}", threads);
});
std::thread::sleep(std::time::Duration::from_millis(interval_ms));
counter -= 1;
std::thread::sleep(interval);
elapsed += interval;
}

error!("graceful shutdown timed out after 5 seconds, forcing exit");
process::exit(0);
});

rest_server.stop();
Expand Down
Loading