bytedance / monoio

Rust async runtime based on io-uring.
Apache License 2.0
4.02k stars 226 forks source link

How to shutdown the runtime when it is running the block_on method? #275

Open lsk569937453 opened 4 months ago

lsk569937453 commented 4 months ago

Is your feature request related to a problem? Please describe. A clear and concise description of what the problem is that I want to shutdown the runtime when it is running on the block_on.

Describe the solution you'd like Maybe we can pass the receiver of channle to the runtime when construct it.

Describe alternatives you've considered A clear and concise description of any alternative solutions or features you've considered.

Additional context

std::thread::spawn(move || {
            let (stop_tx, stop_rx) = channel();
            let mut rt = monoio::RuntimeBuilder::<monoio::IoUringDriver>::new()
                .with_entries(256)
                .enable_timer()
                .build()
                .unwrap();
            rt.block_on(async { main_with_error().await });
        });

Maybe we can pass the stop_rx to the Runtimebuilder,and the runtime will shutdown when the stop_rx receive the message.

ihciah commented 2 months ago

Current task JoinHandle's output is not something like Result which means the main task can be interrupted, instead, it is returned only if the main task finishes normally. This can be implemented as you mentioned, but to make it "zero-cost", I think users can do it manually. For example, create a channel and use select!{channel, original_main_task} as main task. In this way, there's no cost for users which hope it can be run forever, or, hope can exit on certain other things like signal instead of the channel.