actix / actix-net

A collection of lower-level libraries for composable network services.
https://actix.rs
Apache License 2.0
706 stars 345 forks source link

Consider allowing a `SystemRunner::block_on()` future to listen for `System::stop()` #588

Open finnbear opened 1 month ago

finnbear commented 1 month ago

I use SystemRunner::block_on() in my main function to run my http server. It is ergonomic since it passes through my std::process::ExitCode return value from my future. Now I would like to use System::current().stop() to shutdown my server. Unfortunately, I don't see a way to await the stop event within my block_on future.

One possible design:

fn main() -> ExitCode {
    let (runtime, stop) = System::new().into_parts(); // or `into_runtime_and_stop()`
    runtime.block_on(async move {
        let http_server = axum::serve(...System::current().stop()...);
        tokio::select {
            _ = stop => {
                return ExitCode::SUCCESS;
            }
            _ = http_server => {
                 return ExitCode::FAILURE;
            }
        }
    })
}

I considered multiple workarounds, and am currently constructing an extra Oneshot channel and passing it around my application (losing the benefit of System::current().stop() working anywhere).