I wanted to start my web service at runtime, so I wrote the following code. However, the first startup succeeded, and the second startup always failed. I suspect that resources are occupied after the first shutdown of the web service. Who can tell me what's going on and how I can solve this problem?
fn main() {
test_start();
println!("Restart...");
test_start();
}
fn test_start() {
let (tx, rx) = mpsc::channel();
let thd = thread::spawn(move || {
let sys = actix_rt::System::new("test");
let srv = HttpServer::new(|| {
App::new().service(
web::resource("/").route(web::to(|| HttpResponse::Ok().body("test"))),
)
})
.system_exit()
.disable_signals()
.bind("127.0.0.1:8080")
.unwrap()
.run();
let _ = tx.send((srv, actix_rt::System::current()));
let _ = sys.run();
});
let (srv, sys) = rx.recv().unwrap();
println!("Server is running.");
thread::sleep(Duration::from_millis(10000));
// stop
let _ = srv.stop(false);
thread::sleep(Duration::from_millis(1000));
let _ = sys.stop();
thd.join();
println!("Server stopped.");
}