fix(database): use conservative block_in_place strategy to prevent deadlock#3251
Merged
rakita merged 1 commit intobluealloy:mainfrom Dec 23, 2025
Merged
Conversation
async_db block_in_place usage
CodSpeed Performance ReportMerging #3251 will not alter performanceComparing Summary
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Reverts a broken pointer comparison in
HandleOrRuntime::block_onthat was intended to optimize different-runtime scenarios but instead reintroduced deadlock risk for the common same-runtime case.Background: The Original Change
A recent change(#3212) added pointer comparison to detect same-runtime scenarios:
The stated intent was:
The idea was:
block_in_place(prevent deadlock)block_in_place(assumed unnecessary)Problem 1: The Implementation is Broken
The
core::ptr::eqcomparison always returnsfalsebecause it compares memory addresses of two differentHandlestruct instances, not their logical runtime identity:handleis stored in theWrapDatabaseAsyncstruct (heap address)currentis returned byHandle::try_current()(stack address)Result:
block_in_placeis NEVER used, even for same-runtime scenarios.Impact of Broken Code
block_in_placeblock_in_placeblock_in_placeblock_in_placeThe broken code reintroduces deadlock in the common case while "fixing" an edge case.
This can be reproduced by running
cargo run -p example-block-traces. Below is the error log:Same for some other examples codes, like
cargo run -p example-uniswap-get-reserves.Problem 2: The Original Concern is Unfounded
The original change assumed that using
block_in_placewith a different-runtime handle is problematic.However,
block_in_placeis about freeing up the current runtime's worker thread, not about which runtime we're blocking on:Solution
Replace the broken pointer comparison with a conservative approach: use
block_in_placefor ALL multi-threaded runtime contexts.The implemented logic is technically sound and follows one of the 'Bridge' patterns recommended by Tokio. Even when calling
block_onon a Handle from a different runtime,block_in_placewill handle it safely as long as the current thread is a worker in a multi-threaded runtimeWhy This is Correct
block_in_placeblock_in_placeblock_ondirectlyblock_in_placewould panic)block_ondirectlyRelated
#1056