Repository navigation
sqlite: fix lock_handle deadlock - #4559
Open
sainathr19 wants to merge 2 commits into
Open
sainathr19 wants to merge 2 commits into
sainathr19 wants to merge 2 commits into
Conversation
Back-to-back lock_handle() calls can leave stale UnlockDb commands in the worker's bounded channel; once it fills, the caller and worker deadlock. This test hangs on main.
Ping the worker before sending UnlockDb so it has finished every earlier command and holds the lock again. The caller's lock() then always queues behind the worker and each UnlockDb is consumed, so stale ones can't fill the command channel.
sainathr19
marked this pull request as ready for review
November 27, 2026 10:21
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.
Fixes #4557.
Problem
The worker only releases the connection mutex while handling
UnlockDb, then re-locks it. If the worker is descheduled between releasing and re-locking, the nextlock_handle()gets the free lock immediately and itsUnlockDbis never consumed. Repeated calls pile these up until the bounded command channel is full; then the caller holds the lock while blocked onsend_async, and the worker blocks on the lock.Fix
unlock_dbnow pings the worker before sendingUnlockDb. Commands are processed in order and the worker re-locks before moving to the next one, so once the ping is answered the worker holds the lock with nothing queued. The caller'slock()then always queues behind the worker and eachUnlockDbis consumed. A staleUnlockDbfrom a cancelled call is drained before the ping is answered, so it can't break the next call either.Assumptions
The fix relies on the same invariants the existing hand-over already does:
UnlockDbhandler and re-locks before the next command;joinregisters the caller'slock()before theUnlockDbis sent;lock_handle(via&mut self) take the lock.Cancelling
lock_handle()at any point is safe: before the send nothing is queued, and a staleUnlockDbleft after it is drained by the next call's ping.Cost: one extra round-trip per
lock_handle(), roughly +6 µs in a debug build (6.4 → 12.9 µs per call).Test
Adds a regression test that hangs on
main(3/3 runs) and passes with the fix (3/3):It takes ~40s and keeps all cores busy to widen the race window; happy to trim it or mark it
#[ignore]if that's too heavy for CI.