Repository navigation
fix(DBDriver): resolve lock-order inversion between dbSafeAccess and trashes mutexes - #1775
Conversation
…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
left a comment
There was a problem hiding this comment.
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_trashesMutexbefore 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 callsloadSignatures()during that gap:
- It checks the trash, which is already cleared, so it doesn't find the signature.
- It gets
_dbSafeAccessMutexbeforeemptyTrashes()does and queries the database.- The database doesn't have the signature yet, so the load returns nothing.
The same applies to
loadWords,getNodeData,loadLinks,getWeightand 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, thenDBDriverSqlite3::loadQuery()calls the publicgetLastWordId(), 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 inemptyTrashes()is back. InloadQuery(), I replaced the call withgetLastIdQuery("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._dbSafeAccessMutexis a recursiveUMutex, so re-locking it on that path was never a problem.
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, thenloadQuery()→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
_dbSafeAccessMutexonly after releasing_trashesMutexinemptyTrashes(). This removes the nested acquisition entirely and makesemptyTrashes()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
_dbSafeAccessMutexas before; any item added to trash in the tiny window between the two locks is simply drained on the nextemptyTrashes()pass (asasyncSavealready permits).Testing
load()path, so the cycle is broken by construction.Closes #1765.