Skip to content

Commit

Permalink
fix(server): join on thread when Listening drops
Browse files Browse the repository at this point in the history
Closes #447
  • Loading branch information
seanmonstar committed Apr 15, 2015
1 parent f246c6a commit b7ecdf5
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ S: NetworkStream + Clone + Send> Server<'a, H, L> {
let guard = thread::spawn(move || pool.accept(work, threads));

Ok(Listening {
_guard: guard,
_guard: Some(guard),
socket: socket,
})
}
Expand Down Expand Up @@ -176,13 +176,24 @@ where S: NetworkStream + Clone, H: Handler {
/// A listening server, which can later be closed.
pub struct Listening {
#[cfg(feature = "nightly")]
_guard: JoinHandle<()>,
_guard: Option<JoinHandle<()>>,
#[cfg(not(feature = "nightly"))]
_guard: JoinHandle,
_guard: Option<JoinHandle>,
/// The socket addresses that the server is bound to.
pub socket: SocketAddr,
}

impl Drop for Listening {
fn drop(&mut self) {
match self._guard.take() {
Some(guard) => {
let _ = guard.join();
},
None => ()
}
}
}

impl Listening {
/// Stop the server from listening to its socket address.
pub fn close(&mut self) -> HttpResult<()> {
Expand Down

0 comments on commit b7ecdf5

Please sign in to comment.