Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ckb graceful shutdown #3994

Closed
zhangsoledad opened this issue May 25, 2023 · 2 comments · Fixed by #3999
Closed

Fix ckb graceful shutdown #3994

zhangsoledad opened this issue May 25, 2023 · 2 comments · Fixed by #3999
Labels
t:bug Type: This doesn't seem right.

Comments

@zhangsoledad
Copy link
Member

Because the official documentation is misleading, tokio-rs/tokio#4231, resulting in the current graceful shutdown mechanism is actually incorrect, need to fix and explore how to simplify.

@zhangsoledad zhangsoledad added the t:bug Type: This doesn't seem right. label May 25, 2023
@eval-exec

This comment was marked as outdated.

@eval-exec
Copy link
Collaborator

I'm going to create a tokio::sync::mpsc channel, and let all tokio tasks get the ownership of a Sender, and wait the Receiver in fn main(), when all tokio tasks finished, Sender will be dropped.

And use tokio_util::sync::CancellationToken to notify all tokio tasks to exit.

use tokio::sync::mpsc::{channel, Sender};
use tokio::time::{sleep, Duration};

#[tokio::main]
async fn main() {
    let (send, mut recv) = channel(1);

    for i in 0..10 {
        tokio::spawn(some_operation(i, send.clone()));
    }

    // Wait for the tasks to finish.
    //
    // We drop our sender first because the recv() call otherwise
    // sleeps forever.
    drop(send);

    // When every sender has gone out of scope, the recv call
    // will return with an error. We ignore the error.
    let _ = recv.recv().await;
}

async fn some_operation(i: u64, _sender: Sender<()>) {
    sleep(Duration::from_millis(100 * i)).await;
    println!("Task {} shutting down.", i);

    // sender goes out of scope ...
}

Ref: "Waiting for things to finish shutting down" Section on tokio shutdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
t:bug Type: This doesn't seem right.
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

2 participants