Skip to content

How to cancel a UDP stream? #3357

Answered by Darksonn
tones111 asked this question in Q&A
Dec 27, 2020 · 1 comments · 1 reply
Discussion options

You must be logged in to vote

Unless you are still on Tokio 0.2.x, the JoinHandle type returned by tokio::spawn has an rx_handle.abort method you can call. However, please note that this will not cause the println! after the loop to print anything because it kills the entire task, not just the loop.

If you wish to cancel just the loop and still perform cleanup, you can do the following:

let (kill, recv_kill) = tokio::sync::oneshot::channel();

tokio::spawn(async move {
    tokio::select! {
        _ = the_loop(rx) => {},
        _ = recv_kill => {},
    }
    println!("Shutting down...");
});

async fn the_loop(rx: SplitStream<UdpFramed<ACodec>>) {
    while let Some(_resp) = rx.next().await {
        println!("Response"

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@tones111
Comment options

Answer selected by tones111
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants