Skip to content

Commit 30e9fd9

Browse files
committed
Release handles only at appropriate times
1 parent 1ecc650 commit 30e9fd9

File tree

2 files changed

+24
-11
lines changed
  • ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Storage/LedgerDB

2 files changed

+24
-11
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -660,9 +660,8 @@ newForkerByRollback h rr n = getEnv h $ \ldbEnv ->
660660

661661
closeForkerEnv ::
662662
IOLike m => ForkerEnv m l blk -> m ()
663-
closeForkerEnv ForkerEnv{foeLedgerDbLock, foeCleanup, foeInitialHandleKey} =
663+
closeForkerEnv ForkerEnv{foeLedgerDbLock, foeCleanup} =
664664
RAWLock.withWriteAccess foeLedgerDbLock $ \() -> do
665-
Monad.void $ release foeInitialHandleKey
666665
Monad.join $ readTVarIO foeCleanup
667666
pure ((), ())
668667

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

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ module Ouroboros.Consensus.Storage.LedgerDB.V2.Forker
1919
, module Ouroboros.Consensus.Storage.LedgerDB.Forker
2020
) where
2121

22+
import qualified Control.Monad as Monad
2223
import Control.RAWLock (RAWLock)
2324
import Control.ResourceRegistry
2425
import Control.Tracer
@@ -158,24 +159,37 @@ implForkerCommit env = do
158159
stateTVar
159160
foeSwitchVar
160161
( \(LedgerSeq olddb) -> fromMaybe theImpossible $ do
162+
{-
163+
An example of the flow in this complicated switch:
164+
165+
olddb: A :| [ B, C, D ]
166+
toKeepLdb: A :| [ B, C ]
167+
toCloseLdb: C :| [ D ]
168+
169+
lseq: C' :| [ E, F ]
170+
171+
newdb: A :| [ B, C, E, F ]
172+
transfer: [ E, F ]
173+
to close: [ D ]
174+
-}
175+
161176
-- Split the selection at the intersection point. The snd component will
162177
-- have to be closed.
163-
(toKeepBase, toCloseLdb) <- AS.splitAfterMeasure intersectionSlot (either predicate predicate) olddb
164-
(toCloseForker, toKeepTip) <-
165-
AS.splitAfterMeasure intersectionSlot (either predicate predicate) lseq
166-
-- Join the prefix of the selection with the sequence in the forker
167-
newdb <- AS.join (const $ const True) toKeepBase toKeepTip
168-
-- Do /not/ close the anchor of @toClose@, as that is also the
169-
-- tip of @olddb'@ which will be used in @newdb@.
178+
(toKeepLdb, toCloseLdb) <- AS.splitAfterMeasure intersectionSlot (either predicate predicate) olddb
179+
-- Do /not/ close the anchor of @toCloseLdb@, as that is also the
180+
-- tip of @toKeepBase@ which will be used in @newdb@.
170181
let ldbToClose = case toCloseLdb of
171182
AS.Empty _ -> Nothing
172183
_ AS.:< closeOld' -> Just (LedgerSeq closeOld')
173184
transferCommitted = do
174-
closeLedgerSeq (LedgerSeq toCloseForker)
185+
Monad.void $ release (foeInitialHandleKey env)
175186

176187
-- All the other remaining handles are transferred to the LedgerDB registry
177188
keys <- transferRegistry foeResourceRegistry foeLedgerDbRegistry
178-
mapM_ (\(k, v) -> transfer (tables v) k) $ zip keys (AS.toOldestFirst toKeepTip)
189+
mapM_ (\(k, v) -> transfer (tables v) k) $ zip keys (AS.toOldestFirst lseq)
190+
191+
-- Join the prefix of the selection with the sequence in the forker
192+
newdb <- AS.join (const $ const True) toKeepLdb lseq
179193

180194
pure ((transferCommitted, ldbToClose), LedgerSeq newdb)
181195
)

0 commit comments

Comments
 (0)