Skip to content

Commit 06db287

Browse files
committed
LedgerDB.V2: prevent race condition when snapshotting
Hold a read lock while getting and duplicating the anchor handle, such that the handle can not be closed in between. Also make sure to close the duplicated handle.
1 parent 9468967 commit 06db287

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
### Patch
2+
3+
- LedgerDB.V2: prevent race condition when creating snapshots.

ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Storage/LedgerDB/V2.hs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -368,13 +368,17 @@ implTryTakeSnapshot ::
368368
implTryTakeSnapshot bss env mTime nrBlocks =
369369
if onDiskShouldTakeSnapshot (ldbSnapshotPolicy env) (uncurry (flip diffTime) <$> mTime) nrBlocks
370370
then do
371-
Monad.void
372-
. takeSnapshot
373-
(configCodec . getExtLedgerCfg . ledgerDbCfg $ ldbCfg env)
374-
(LedgerDBSnapshotEvent >$< ldbTracer env)
375-
(ldbHasFS env)
376-
. anchorHandle
377-
=<< readTVarIO (ldbSeq env)
371+
let getStateRef =
372+
RAWLock.withReadAccess (ldbOpenHandlesLock env) $ \LDBLock -> do
373+
stateRef <- anchorHandle <$> readTVarIO (ldbSeq env)
374+
tables' <- duplicate $ tables stateRef
375+
pure stateRef{tables = tables'}
376+
bracket getStateRef (close . tables) $
377+
Monad.void
378+
. takeSnapshot
379+
(configCodec . getExtLedgerCfg . ledgerDbCfg $ ldbCfg env)
380+
(LedgerDBSnapshotEvent >$< ldbTracer env)
381+
(ldbHasFS env)
378382
Monad.void $
379383
trimSnapshots
380384
(LedgerDBSnapshotEvent >$< ldbTracer env)

0 commit comments

Comments
 (0)