Rebuild can dispose engine services after a commit while ReleaseTransaction is still cleaning up
Summary
This is a pre-existing dev race, not introduced by #3027.
TransactionMonitor.ReleaseTransaction releases the transaction admission lease before it finishes thread-local cleanup and cache trimming. A concurrent Rebuild() can acquire exclusive admission in that window, close/dispose the monitor and disk, and make an already-persisted commit throw from release cleanup (for example ObjectDisposedException).
#2965 fixed the earlier/larger race where Rebuild could enter while a writer was still active. This is a narrower tail race that remains after that fix.
A proposed implementation existed in #3037; that PR was closed without merge.
Failure sequence
- A writer reaches the end of commit and the transaction is already persisted.
ReleaseTransactioncalls_locker.ExitTransaction(transaction.OwnerThread).- Before it clears
_slot.Value/ trims the disk cache, a waitingRebuild()acquires exclusive admission. - Rebuild closes/disposes engine services.
- The original writer resumes cleanup and touches disposed state, so a successful commit can surface as an exception.
Expected invariant
Releasing transaction admission must be the last step that can touch transaction-monitor/disk state which Rebuild may dispose.
A commit that has already been durably persisted must not fail because Rebuild disposed services during release cleanup.
Suggested fix
Finish thread-local slot cleanup and cache trimming while the transaction lease still blocks exclusive admission, then release the lease in an inner finally so it is always released even if cleanup throws.
Regression evidence
Add/retain a deterministic interleaving test that parks ReleaseTransaction immediately before its cleanup, starts Rebuild, and proves Rebuild cannot dispose the services until cleanup is complete.
Related: #2965, #3037, #3036.
Source: litedb-org/LiteDB