Skip to content

Conversation

jif-oai
Copy link
Contributor

@jif-oai jif-oai commented Sep 18, 2025

Why Use tokio::sync::Mutex

std::sync::Mutex are not async-aware. As a result, they will block the entire thread instead of just yielding the task. Furthermore they can be poisoned which is not the case of tokio Mutex.
This allows the Tokio runtime to continue running other tasks while waiting for the lock, preventing deadlocks and performance bottlenecks.

In general, this is preferred in async environment

Copy link
Collaborator

@bolinfest bolinfest left a comment

Choose a reason for hiding this comment

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

Thanks for doing this!

@@ -3356,7 +3366,7 @@ mod tests {
}),
));

let actual = session.state.lock_unchecked().history.contents();
let actual = tokio_test::block_on(async { session.state.lock().await.history.contents() });
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should we be switching to tokio::test instead for these?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will switch all the tests in a dedicated PR. Let's keep them small for now :)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Sounds great: I <3 small, specific PRs!

@jif-oai jif-oai merged commit 277fc62 into main Sep 18, 2025
19 checks passed
@jif-oai jif-oai deleted the jif/mutex branch September 18, 2025 17:21
@github-actions github-actions bot locked and limited conversation to collaborators Sep 18, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants