0

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.");
}
ljhiiii
  • 1
  • 1
  • 2
    Can you show an error backtrace or describe the failure behavior in more detail? [This](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=471b3df4502f92678860f5b519287dc9) works just fine for me. (On linux. Maybe that's relevant.) – Caesar Jan 24 '22 at 01:31
  • I run on Windows platform. Moreover, it is normal to replace the port when starting the service for the second time – ljhiiii Jan 24 '22 at 04:01
  • So… can you edit your post and add the error message / stack trace you're seeing? – Caesar Jan 24 '22 at 11:04
  • Sorry, I don't know how to get stack trace. That's all my code. Output is: server is running. server stopped. restart... server is running. server stopped. – ljhiiii Jan 24 '22 at 11:33
  • Normally, you can collect a stack trace by `set RUST_BACKTRACE=1` in cmd before running your code. That will print a backtrace when your code panics. But your comment sounds like the code is not panicking but running fine. – Caesar Jan 24 '22 at 11:38
  • What exactly is the behavior you were expecting, compared to what you're seeing now? – Caesar Jan 24 '22 at 11:38

1 Answers1

0

actix_web::dev::Server.stop() does not work on windows #1249

Peter Csala
  • 17,736
  • 16
  • 35
  • 75
ljhiiii
  • 1
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 25 '22 at 05:24