Skip to content

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

Open
@finnbear

Description

@finnbear

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requesthelp wantedExtra attention is needed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions