Skip to content

sqlite: fix lock_handle deadlock - #4559

Open
sainathr19 wants to merge 2 commits into
transact-rs:mainfrom
sainathr19:sqlite-fix-lock-handle-deadlock
Open

sainathr19 wants to merge 2 commits into
transact-rs:mainfrom
sainathr19:sqlite-fix-lock-handle-deadlock

Conversation

@sainathr19

@sainathr19 sainathr19 commented Nov 27, 2026 •

Copy link
Copy Markdown

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 next lock_handle() gets the free lock immediately and its UnlockDb is never consumed. Repeated calls pile these up until the bounded command channel is full; then the caller holds the lock while blocked on send_async, and the worker blocks on the lock.

Fix

unlock_db now pings the worker before sending UnlockDb. 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's lock() then always queues behind the worker and each UnlockDb is consumed. A stale UnlockDb from 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:

  • the worker only releases the lock inside the UnlockDb handler and re-locks before the next command;
  • commands are processed in order;
  • the mutex is fair, and join registers the caller's lock() before the UnlockDb is sent;
  • only the worker and lock_handle (via &mut self) take the lock.

Cancelling lock_handle() at any point is safe: before the send nothing is queued, and a stale UnlockDb left 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):

cargo test --features sqlite,runtime-tokio --test sqlite -- --exact it_does_not_deadlock_on_back_to_back_lock_handle

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.

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
sainathr19 marked this pull request as ready for review November 27, 2026 10:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sqlite: back-to-back lock_handle calls can deadlock the connection (stale UnlockDb commands fill the channel)

1 participant