Skip to content

fix(DBDriver): resolve lock-order inversion between dbSafeAccess and trashes mutexes - #1775

Merged
matlabbe merged 4 commits into
introlab:masterfrom
webzuweb:fix/dbdriver-lock-order-inversion
Sep 28, 2026
Merged

matlabbe merged 4 commits into
introlab:masterfrom
webzuweb:fix/dbdriver-lock-order-inversion

Conversation

@webzuweb

Copy link
Copy Markdown
Contributor

Problem

ThreadSanitizer reports a lock-order inversion in DBDriver (issue #1765). The two mutexes protecting database access (_dbSafeAccessMutex) and the in-memory trash containers (_trashesMutex) are acquired in opposite orders:

  • load() path: DBDriver::load() holds _dbSafeAccessMutex, then loadQuery() → getLastWordId() acquires _trashesMutex → order M0 → M1.
  • emptyTrashes(): holds _trashesMutex, then acquires _dbSafeAccessMutex → order M1 → M0.

This forms the cycle M0 => M1 => M0, which TSan flags as a potential deadlock.

Fix

Acquire _dbSafeAccessMutex only after releasing _trashesMutex in emptyTrashes(). This removes the nested acquisition entirely and makes emptyTrashes() follow the same sequential "look in trash, then database" pattern already used by every other DBDriver accessor (getLastWordId, getLastMapId, getInvertedIndexNi, getAllLabels, …).

The change is a pure reorder — no critical-section contents changed, so database/trash consistency semantics are preserved. Trash items swapped into local maps are still saved under _dbSafeAccessMutex as before; any item added to trash in the tiny window between the two locks is simply drained on the next emptyTrashes() pass (as asyncSave already permits).

Testing

  • No behavioral change: 2-line reorder only.
  • I did not have a TSan-enabled build locally (macOS, no ROS2/TSan toolchain); CI + a TSan run by the reporter (hwk) would confirm the warning is gone. The fix matches the lock-order direction of the pre-existing load() path, so the cycle is broken by construction.

Closes #1765.

webzuweb and others added 4 commits September 27, 2026 16:07
…trashes mutexes

emptyTrashes() acquired _dbSafeAccessMutex while holding _trashesMutex
(M1->M0), whereas the load() path acquires _dbSafeAccessMutex and then,
inside loadQuery()->getLastWordId(), acquires _trashesMutex (M0->M1).
This opposite nesting forms a lock-order cycle that ThreadSanitizer flags
as a potential deadlock.

Acquire _dbSafeAccessMutex only after releasing _trashesMutex, matching
the sequential 'look in trash, then database' pattern used by every other
DBDriver accessor (getLastWordId, getLastMapId, getInvertedIndexNi, ...).

Refs: introlab#1765

@matlabbe matlabbe left a comment

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.

There was a reason why we interleaved the locks in emptyTrashes(), the real fix was to make load() not call the public function getLastWordId() (locking also _trashesMutex inside protected _dbSafeAccessMutex), which was causing the deadlock cited in #1765

Here the details from claude:

The race the PR adds. In emptyTrashes(), the PR releases _trashesMutex before taking _dbSafeAccessMutex. In the gap between the two, the signatures are no longer in the trash and not yet in the database. If another thread calls loadSignatures() during that gap:

  1. It checks the trash, which is already cleared, so it doesn't find the signature.
  2. It gets _dbSafeAccessMutex before emptyTrashes() does and queries the database.
  3. The database doesn't have the signature yet, so the load returns nothing.

The same applies to loadWords, getNodeData, loadLinks, getWeight and the other "check trash, then database" accessors. The original nesting is what closes that gap. The PR description says the pattern is sequential, but that only works because the writer holds the lock across both steps.

Where the inversion actually comes from. The ThreadSanitizer report in #1765 shows the other lock order isn't a general load path. It's one call: DBDriver::load() takes _dbSafeAccessMutex, then DBDriverSqlite3::loadQuery() calls the public getLastWordId(), which takes _trashesMutex. That's the only place the two locks are taken in that order.

The fix. I reverted the PR's change to DBDriver.cpp, so the original nesting in emptyTrashes() is back. In loadQuery(), I replaced the call with getLastIdQuery("Word", id), which reads the database directly, and added a comment explaining why.

The only difference is that the direct query skips the trash lookup. That shouldn't matter: load() is only called at startup, on a driver that has just been opened (Memory.cpp:464, Memory.cpp:475, VWDictionary.cpp:211), so the word trash is empty then. _dbSafeAccessMutex is a recursive UMutex, so re-locking it on that path was never a problem.

@matlabbe
matlabbe merged commit c59e0d7 into introlab:master Sep 28, 2026
35 checks passed
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.

[Potential deadlock] DBDriver uses inconsistent lock ordering for database and trash access

2 participants