Browse Source

Exit RPC server during its d-tor

refactor-mempool
Roman Zeyde 7 years ago
parent
commit
3f9f6e6330
No known key found for this signature in database GPG Key ID: 87CAE5FA46917CBB
  1. 1
      src/bin/electrs.rs
  2. 12
      src/rpc.rs

1
src/bin/electrs.rs

@ -46,7 +46,6 @@ fn run_server(config: &Config) -> Result<()> {
break;
}
}
server.map(|s| s.exit());
Ok(())
}

12
src/rpc.rs

@ -408,7 +408,7 @@ pub enum Notification {
pub struct RPC {
notification: Sender<Notification>,
server: thread::JoinHandle<()>,
server: Option<thread::JoinHandle<()>>, // so we can join the server while dropping this ojbect
}
struct Stats {
@ -468,7 +468,7 @@ impl RPC {
let notification = Channel::new();
let handle = RPC {
notification: notification.sender(),
server: spawn_thread("rpc", move || {
server: Some(spawn_thread("rpc", move || {
let senders = Arc::new(Mutex::new(Vec::<SyncSender<Message>>::new()));
let acceptor = RPC::start_acceptor(addr);
RPC::start_notifier(notification, senders.clone(), acceptor.sender());
@ -492,7 +492,7 @@ impl RPC {
for child in children {
let _ = child.join();
}
}),
})),
};
handle
}
@ -500,9 +500,11 @@ impl RPC {
pub fn notify(&self) {
self.notification.send(Notification::Periodic).unwrap();
}
}
pub fn exit(self) {
impl Drop for RPC {
fn drop(&mut self) {
self.notification.send(Notification::Exit).unwrap();
self.server.join().unwrap();
self.server.take().map(|t| t.join().unwrap());
}
}

Loading…
Cancel
Save