Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Clean up threads and sockets at the same time as the thread cleaner #49

Merged
merged 1 commit into from
Sep 27, 2023
Merged
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
13 changes: 5 additions & 8 deletions src/electrum/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,6 @@ impl RPC {
server: Some(spawn_thread("rpc", move || {
let senders =
Arc::new(Mutex::new(Vec::<crossbeam_channel::Sender<Message>>::new()));
let killers = Arc::new(Mutex::new(Vec::<Sender<()>>::new()));

let acceptor_shutdown = Channel::unbounded();
let acceptor_shutdown_sender = acceptor_shutdown.sender();
Expand All @@ -799,7 +798,6 @@ impl RPC {

// Kill the peers properly
let (killer, peace_receiver) = std::sync::mpsc::channel();
killers.lock().unwrap().push(killer);

#[cfg(feature = "electrum-discovery")]
let discovery = discovery.clone();
Expand All @@ -823,10 +821,11 @@ impl RPC {
});

trace!("[{}] spawned {:?}", addr, spawned.thread().id());
threads.insert(spawned.thread().id(), spawned);
threads.insert(spawned.thread().id(), (spawned, killer));
while let Ok(id) = garbage_receiver.try_recv() {
if let Some(thread) = threads.remove(&id) {
if let Some((thread, killer)) = threads.remove(&id) {
trace!("[{}] joining {:?}", addr, id);
let _ = killer.send(());
if let Err(error) = thread.join() {
error!("failed to join {:?}: {:?}", id, error);
}
Expand All @@ -838,15 +837,13 @@ impl RPC {
drop(garbage_receiver);

trace!("closing {} RPC connections", senders.lock().unwrap().len());
for killer in killers.lock().unwrap().iter() {
let _ = killer.send(());
}
for sender in senders.lock().unwrap().iter() {
let _ = sender.try_send(Message::Done);
}

for (id, thread) in threads {
for (id, (thread, killer)) in threads {
trace!("joining {:?}", id);
let _ = killer.send(());
if let Err(error) = thread.join() {
error!("failed to join {:?}: {:?}", id, error);
}
Expand Down