fix(server): join on thread when Listening drops

Closes #447
This commit is contained in:
Sean McArthur
2015-04-15 16:27:43 -07:00
parent f246c6a4a8
commit 68d4d63c2a

View File

@@ -107,7 +107,7 @@ S: NetworkStream + Clone + Send> Server<'a, H, L> {
let guard = thread::spawn(move || pool.accept(work, threads)); let guard = thread::spawn(move || pool.accept(work, threads));
Ok(Listening { Ok(Listening {
_guard: guard, _guard: Some(guard),
socket: socket, socket: socket,
}) })
} }
@@ -176,16 +176,23 @@ where S: NetworkStream + Clone, H: Handler {
/// A listening server, which can later be closed. /// A listening server, which can later be closed.
pub struct Listening { pub struct Listening {
#[cfg(feature = "nightly")] #[cfg(feature = "nightly")]
_guard: JoinHandle<()>, _guard: Option<JoinHandle<()>>,
#[cfg(not(feature = "nightly"))] #[cfg(not(feature = "nightly"))]
_guard: JoinHandle, _guard: Option<JoinHandle>,
/// The socket addresses that the server is bound to. /// The socket addresses that the server is bound to.
pub socket: SocketAddr, pub socket: SocketAddr,
} }
impl Drop for Listening {
fn drop(&mut self) {
let _ = self._guard.take().map(|g| g.join());
}
}
impl Listening { impl Listening {
/// Stop the server from listening to its socket address. /// Stop the server from listening to its socket address.
pub fn close(&mut self) -> HttpResult<()> { pub fn close(&mut self) -> HttpResult<()> {
let _ = self._guard.take();
debug!("closing server"); debug!("closing server");
//try!(self.acceptor.close()); //try!(self.acceptor.close());
Ok(()) Ok(())