From 284275e2d1acf5f5b788383312c7d952ffd5c13e Mon Sep 17 00:00:00 2001 From: Roman Zeyde Date: Thu, 14 Jun 2018 00:07:35 +0300 Subject: [PATCH] Stop and join all Electrum client threads on SIGINT --- src/rpc.rs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/rpc.rs b/src/rpc.rs index 9fc4c9d..967487e 100644 --- a/src/rpc.rs +++ b/src/rpc.rs @@ -421,19 +421,26 @@ impl RPC { let senders = Arc::new(Mutex::new(Vec::>::new())); let acceptor = RPC::start_acceptor(addr); RPC::start_notifier(notification, senders.clone(), acceptor.sender()); + let mut children = vec![]; while let Some((stream, addr)) = acceptor.receiver().recv().unwrap() { let query = query.clone(); let senders = senders.clone(); let stats = stats.clone(); - thread::spawn(move || { + children.push(thread::spawn(move || { info!("[{}] connected peer", addr); let conn = Connection::new(query, stream, addr, stats); senders.lock().unwrap().push(conn.chan.sender()); conn.run(); info!("[{}] disconnected peer", addr); - }); + })); + } + info!("stopping RPC server"); + for sender in senders.lock().unwrap().iter() { + let _ = sender.send(Message::Done); + } + for child in children { + let _ = child.join(); } - info!("stopping RPC server") }), }; handle