Skip to content

Document possible Mutex deadlock in block_in_place#7899

Open
fuzzypixelz wants to merge 1 commit intotokio-rs:masterfrom
fuzzypixelz:resolve-issue-7892
Open

Document possible Mutex deadlock in block_in_place#7899
fuzzypixelz wants to merge 1 commit intotokio-rs:masterfrom
fuzzypixelz:resolve-issue-7892

Conversation

@fuzzypixelz
Copy link
Copy Markdown

Resolves #7892.

@fuzzypixelz
Copy link
Copy Markdown
Author

fuzzypixelz commented Feb 9, 2026

Rendered:

image

All links are valid.

I found the MRE too complicated to include in the Rustdoc string, so I linked the issue instead. I couldn't find any existing instances where documentation links to closed issues so I don't know how appropriate this is.

cc @Darksonn.

Comment on lines 15 to 19
/// Be aware that although this function avoids starving other independently
/// spawned tasks, any other code running concurrently in the same task will
/// be suspended during the call to `block_in_place`. This can happen e.g.
/// when using the [`join!`] macro. To avoid this issue, use
/// [`spawn_blocking`] instead of `block_in_place`.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already have this paragraph.

The issue is not really specific to Mutex. It happens with lots of stuff, but you make it sound like the only problem is Mutex. If you want to write something that talks about Mutex, it needs to be clear that it's an example and that it applies to any other code running in the same task.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already have this paragraph.

I understand that the two paragraphs are perhaps too similar, my intension was for the added paragraph to have the extra context of block_on use. I don't mind merging the second paragraph into the first, I'm just not yet sure if spawn_blocking is a good workaround for this issue as well (if it actually does solve it at all?).

The issue is not really specific to Mutex. It happens with lots of stuff, but you make it sound like the only problem is Mutex.

Right, it seems to me that it happens with Semaphore and RwLock as well. Is there a concept that encompasses all/most possible cases? In either case, I agree Mutex should be marked as an example.

I'll rework this once I have satisfactory answers to both questions.> We already have this paragraph.

I understand that the two paragraphs are perhaps too similar, my intension was for the added paragraph to have the extra context of block_on use. I don't mind merging the second paragraph into the first, I'm just not yet sure if spawn_blocking is a good workaround for this issue as well (if it actually does solve it at all?).

The issue is not really specific to Mutex. It happens with lots of stuff, but you make it sound like the only problem is Mutex.

Right, it seems to me that it happens with Semaphore and RwLock as well. Is there a concept that encompasses all/most possible cases? In either case, I agree Mutex should be marked as an example.

I'll rework this once I have satisfactory answers to both questions.

Copy link
Copy Markdown
Member

@Darksonn Darksonn Feb 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm just not yet sure if spawn_blocking is a good workaround for this issue as well

The most generic thing I can say is "to avoid this, do not use block_in_place". There does not exist one single alternative I can recommend that works in all cases.

I recommend spawn_blocking, which is sometimes a suitable alternative, but it's not identical to block_in_place and so it does not work in all cases where block_in_place can be used.

Is there a concept that encompasses all/most possible cases?

The way I would describe the general problem is "intra-task concurrency". That is to say, the task calling block_in_place is also running any other future concurrently within the same task, using any intra-task concurrency primitive such as select!, join!, FuturesUnordered, buffer_unordered, etc

The bug is a variation of what has been recently dubbed futurelock.

@mattiapitossi mattiapitossi added T-docs Topic: documentation A-tokio Area: The main tokio crate M-task Module: tokio/task S-waiting-on-author Status: awaiting some action (such as code changes) from the PR or issue author. labels Mar 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-tokio Area: The main tokio crate M-task Module: tokio/task S-waiting-on-author Status: awaiting some action (such as code changes) from the PR or issue author. T-docs Topic: documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

block_in_place + block_on + tokio::sync::Mutex may lead to a deadlock

3 participants