Skip to content

Commit dac2f4d

Browse files
committed
Merge pull request #458 from hyperium/server-spawn
fix(server): join on thread when Listening drops
2 parents f246c6a + 68d4d63 commit dac2f4d

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

src/server/mod.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ S: NetworkStream + Clone + Send> Server<'a, H, L> {
107107
let guard = thread::spawn(move || pool.accept(work, threads));
108108

109109
Ok(Listening {
110-
_guard: guard,
110+
_guard: Some(guard),
111111
socket: socket,
112112
})
113113
}
@@ -176,16 +176,23 @@ where S: NetworkStream + Clone, H: Handler {
176176
/// A listening server, which can later be closed.
177177
pub struct Listening {
178178
#[cfg(feature = "nightly")]
179-
_guard: JoinHandle<()>,
179+
_guard: Option<JoinHandle<()>>,
180180
#[cfg(not(feature = "nightly"))]
181-
_guard: JoinHandle,
181+
_guard: Option<JoinHandle>,
182182
/// The socket addresses that the server is bound to.
183183
pub socket: SocketAddr,
184184
}
185185

186+
impl Drop for Listening {
187+
fn drop(&mut self) {
188+
let _ = self._guard.take().map(|g| g.join());
189+
}
190+
}
191+
186192
impl Listening {
187193
/// Stop the server from listening to its socket address.
188194
pub fn close(&mut self) -> HttpResult<()> {
195+
let _ = self._guard.take();
189196
debug!("closing server");
190197
//try!(self.acceptor.close());
191198
Ok(())

0 commit comments

Comments
 (0)