rust-lang / async-book

Asynchronous Programming in Rust
https://rust-lang.github.io/async-book/index.html
MIT License
1.84k stars 248 forks source link

Fix potential deadlock in the executor #189

Closed honzuki closed 4 weeks ago

honzuki commented 1 year ago

spawn and wake are using a version of the send function that "will block until space in the internal buffer becomes available".

That means that if the channel is full, and we try to spawn a new task or a call to wake() was made, send will block the thread, the executor will never get the opportunity to free space in the channel's buffer, and the thread will be stuck in a deadlock.

By using try_send, instead of send, the call will return immediately if the channel's buffer is full, and the call to expect will panic, as expected.