# Tokio channel deadlock

**URL:** <https://internals.rust-lang.org/t/tokio-channel-deadlock/12779>\
**Category:** libs\
**Created:** [July 23, 2020, 12:34pm UTC](https://internals.rust-lang.org/t/tokio-channel-deadlock/12779 "2020-07-23T12:34:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lighty0410](https://avatars.discourse-cdn.com/v4/letter/l/839c29/32.png) [@Lighty0410](https://internals.rust-lang.org/u/Lighty0410)\
**Post date:** [July 23, 2020, 12:34pm UTC](https://internals.rust-lang.org/t/tokio-channel-deadlock/12779/1 "2020-07-23T12:34:07Z")

</div>

I faced some interesting behavior using tokio's channel - `tokio::sync::mpsc` .

Long story short: in some cases, `receiver` doesn't get the value, sent by the `sender` if there's something like loop (or any other heavy computations, i assume?) on the current tokio-thread.

Channel from `std::thread::sync` works perfectly though. I assume it has something to do with either tokio scheduler or cpu context switch ?

Even in the example below it works correctly from time to time. But sometimes it prints "second thread" only when the first thread is dead due to integer overflow.

How can i overcome this problem ? For now i use default `channel()` from `std::thread::sync` . But it seems like a crutch to me.

Example:

```
use tokio::sync::mpsc;

async fn thread1(mut sender: mpsc::Sender<bool>) {
    let mut i = 0;
    loop {
        i += 1;
        if i == 100 {
            sender.send(true).await.unwrap();
        }
    }
}

async fn thread2(mut receiver: mpsc::Receiver<bool>) {
    receiver.recv().await.unwrap();
    println!("second thread");
}

#[tokio::main]
async fn main() {
    let (sender, receiver) = mpsc::channel(1);

    let p1 = tokio::spawn(async move {
        thread1(sender).await;
    });

    let p2 = tokio::spawn(async move {
        thread2(receiver).await;
    });

    tokio::join!(p1, p2);
}

```

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![steffahn](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/steffahn/32/13288_2.png) [@steffahn](https://internals.rust-lang.org/u/steffahn)\
**Post date:** [July 23, 2020, 12:37pm UTC](https://internals.rust-lang.org/t/tokio-channel-deadlock/12779/2 "2020-07-23T12:37:02Z")

</div>

This kind of question is more appropriate in the [users.rust-lang.org](http://users.rust-lang.org) forum.

---

<div class="post-metadata">

**Author:** ![Lighty0410](https://avatars.discourse-cdn.com/v4/letter/l/839c29/32.png) [@Lighty0410](https://internals.rust-lang.org/u/Lighty0410)\
**Post date:** [July 23, 2020, 12:41pm UTC](https://internals.rust-lang.org/t/tokio-channel-deadlock/12779/3 "2020-07-23T12:41:14Z")

</div>

Excuse me!

---

<div class="post-metadata">

**Author:** ![steffahn](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/steffahn/32/13288_2.png) [@steffahn](https://internals.rust-lang.org/u/steffahn)\
**Post date:** [July 23, 2020, 12:46pm UTC](https://internals.rust-lang.org/t/tokio-channel-deadlock/12779/4 "2020-07-23T12:46:50Z")

</div>

To elaborate, this forum is more focused around the compiler and the language _design_. The “users” forum is where lots of help threads are and also all the people who are good at helping out, so feel free to copy your post verbatim to over there. Nontheless, welcome to this forum, too!

---

<div class="post-metadata">

**Author:** ![mbrubeck](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/mbrubeck/32/174_2.png) [@mbrubeck](https://internals.rust-lang.org/u/mbrubeck)\
**Post date:** [July 23, 2020, 4:09pm UTC](https://internals.rust-lang.org/t/tokio-channel-deadlock/12779/5 "2020-07-23T16:09:23Z")

</div>

Closed on July 23, 2020, 4:09pm UTC


