Skip to content

Commit

Permalink
discovery: revise shutdown handling after review
Browse files Browse the repository at this point in the history
  • Loading branch information
wisp3rwind committed Oct 21, 2024
1 parent 1a88825 commit 878d64d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
16 changes: 11 additions & 5 deletions discovery/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ mod server;

use std::{
borrow::Cow,
convert::Infallible,
error::Error as StdError,
pin::Pin,
task::{Context, Poll},
Expand All @@ -39,9 +38,13 @@ pub enum DiscoveryEvent {
ZeroconfError(DiscoveryError),
}

enum ZeroconfCmd {
Shutdown,
}

pub struct DnsSdHandle {
task_handle: tokio::task::JoinHandle<()>,
shutdown_tx: oneshot::Sender<Infallible>,
shutdown_tx: oneshot::Sender<ZeroconfCmd>,
}

impl DnsSdHandle {
Expand All @@ -51,9 +54,12 @@ impl DnsSdHandle {
task_handle,
shutdown_tx,
} = self;
std::mem::drop(shutdown_tx);
let _ = task_handle.await;
log::debug!("Zeroconf responder stopped");
if shutdown_tx.send(ZeroconfCmd::Shutdown).is_err() {
log::warn!("Zeroconf responder unexpectedly disappeared");
} else {
let _ = task_handle.await;
log::debug!("Zeroconf responder stopped");
}
}
}

Expand Down
18 changes: 12 additions & 6 deletions discovery/src/server.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::{
borrow::Cow,
collections::BTreeMap,
convert::Infallible,
net::{Ipv4Addr, Ipv6Addr, SocketAddr, TcpListener},
sync::{Arc, Mutex},
};
Expand Down Expand Up @@ -252,8 +251,12 @@ impl RequestHandler {
}
}

pub(crate) enum DiscoveryServerCmd {
Shutdown,
}

pub struct DiscoveryServer {
close_tx: oneshot::Sender<Infallible>,
close_tx: oneshot::Sender<DiscoveryServerCmd>,
task_handle: tokio::task::JoinHandle<()>,
}

Expand Down Expand Up @@ -324,14 +327,12 @@ impl DiscoveryServer {
});
}
_ = &mut close_rx => {
debug!("Shutting down discovery server");
break;
}
}
}

graceful.shutdown().await;
debug!("Discovery server stopped");
});

Ok(Self {
Expand All @@ -346,7 +347,12 @@ impl DiscoveryServer {
task_handle,
..
} = self;
std::mem::drop(close_tx);
let _ = task_handle.await;
log::debug!("Shutting down discovery server");
if close_tx.send(DiscoveryServerCmd::Shutdown).is_err() {
log::warn!("Discovery server unexpectedly disappeared");
} else {
let _ = task_handle.await;
log::debug!("Discovery server stopped");
}
}
}

0 comments on commit 878d64d

Please sign in to comment.