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

Prevent deadlock by not using futures::executor::block_on #1121

Merged
merged 3 commits into from
Nov 19, 2024

Conversation

shepmaster
Copy link
Member

No description provided.

As Alice Rhyl recommended (slightly reworded)

> Either you can use `tokio::runtime::Handle::block_on` [instead of
> `futures::executor::block_on`] or your program has a subtle
> deadlock.
The request guard works by sending on a channel when it is
dropped. Since we can't do async code in `Drop::drop`, I was using
`futures::executor::block_on` under the (false!) impression that
this *specific* usecase would not be a problem as we are always
pulling from that channel. However, sending on a channel can result in
`Pending` when the Tokio worker's cooperative budget is exhausted,
even if the queue is completely empty!

A simple example:

```rust
// Drain the budget
while task::consume_budget().now_or_never().is_some() {}

// Try to use more budget than we have and block forever
futures::executor::block_on(tx.send(42u8)).unwrap();
```

This `Pending` will never resolve because we are no longer running the
Tokio runtime so it will never see an `.await` and replenish the
budget. With enough bad luck, all the worker threads get stuck like
this and the entire system becomes unavailable.

There are a number of possible workarounds, but the least invasive is
to punt the final logging off to a Tokio task and let it happen
out-of-band. Technically this will make the timing a little bit less
accurate, but I was never worried about exact nanoseconds anyway.
@shepmaster shepmaster added the bug The playground isn't doing what it was intended to label Nov 19, 2024
@shepmaster shepmaster merged commit 5cefccc into main Nov 19, 2024
12 checks passed
@shepmaster shepmaster deleted the deadlock-begone branch November 19, 2024 14:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug The playground isn't doing what it was intended to
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant