actix-net icon indicating copy to clipboard operation
actix-net copied to clipboard

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

Open finnbear opened this issue 6 months ago • 0 comments

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).

finnbear avatar Aug 04 '24 05:08 finnbear