diff --git a/CHANGES.md b/CHANGES.md index 7c02acbf1..84ee4b7cc 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -12,6 +12,8 @@ * StaticFiles decode special characters in request's path +* Fix test server listener leak #654 + ## [0.7.17] - 2018-12-25 ### Added diff --git a/src/test.rs b/src/test.rs index 7039c4fce..f2346115c 100644 --- a/src/test.rs +++ b/src/test.rs @@ -5,7 +5,9 @@ use std::sync::mpsc; use std::{net, thread}; use actix::{Actor, Addr, System}; +use actix::actors::signal; +use actix_net::server::Server; use cookie::Cookie; use futures::Future; use http::header::HeaderName; @@ -66,6 +68,7 @@ pub struct TestServer { ssl: bool, conn: Addr, rt: Runtime, + backend: Addr, } impl TestServer { @@ -112,24 +115,25 @@ impl TestServer { let tcp = net::TcpListener::bind("127.0.0.1:0").unwrap(); let local_addr = tcp.local_addr().unwrap(); - let _ = HttpServer::new(factory) + let srv = HttpServer::new(factory) .disable_signals() .listen(tcp) .keep_alive(5) .start(); - tx.send((System::current(), local_addr, TestServer::get_conn())) + tx.send((System::current(), local_addr, TestServer::get_conn(), srv)) .unwrap(); sys.run(); }); - let (system, addr, conn) = rx.recv().unwrap(); + let (system, addr, conn, backend) = rx.recv().unwrap(); System::set_current(system); TestServer { addr, conn, ssl: false, rt: Runtime::new().unwrap(), + backend, } } @@ -197,6 +201,7 @@ impl TestServer { /// Stop http server fn stop(&mut self) { + let _ = self.backend.send(signal::Signal(signal::SignalType::Term)).wait(); System::current().stop(); } @@ -333,8 +338,7 @@ where .keep_alive(5) .disable_signals(); - tx.send((System::current(), addr, TestServer::get_conn())) - .unwrap(); + #[cfg(any(feature = "alpn", feature = "ssl"))] { @@ -356,18 +360,22 @@ where let tcp = net::TcpListener::bind(addr).unwrap(); srv = srv.listen(tcp); } - srv.start(); + let backend = srv.start(); + + tx.send((System::current(), addr, TestServer::get_conn(), backend)) + .unwrap(); sys.run(); }); - let (system, addr, conn) = rx.recv().unwrap(); + let (system, addr, conn, backend) = rx.recv().unwrap(); System::set_current(system); TestServer { addr, conn, ssl: has_ssl, rt: Runtime::new().unwrap(), + backend, } } }