Skip to content

Commit ae188df

Browse files
committed
Do unsafeRelease when closing all forkers
1 parent 1eabc2b commit ae188df

File tree

3 files changed

+26
-10
lines changed
  • ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Storage/LedgerDB

3 files changed

+26
-10
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,7 @@ closeAllForkers forkersVar = do
705705
forkerEnvs <- Map.elems <$> readTVar forkersVar
706706
writeTVar forkersVar Map.empty
707707
return forkerEnvs
708-
mapM_ closeForkerEnv forkerEnvs
708+
mapM_ (closeForkerEnv (Flag @"ClosingAllForkers" True)) forkerEnvs
709709

710710
-- | Acquire both a value handle and a db changelog at the tip. Holds a read lock
711711
-- while doing so.
@@ -822,4 +822,4 @@ implForkerClose (LDBHandle varState) forkerKey = do
822822
stateTVar
823823
(ldbForkers ldbEnv)
824824
(Map.updateLookupWithKey (\_ _ -> Nothing) forkerKey)
825-
whenJust envMay closeForkerEnv
825+
whenJust envMay (closeForkerEnv (Flag @"ClosingAllForkers" False))

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{-# LANGUAGE DataKinds #-}
12
{-# LANGUAGE DeriveAnyClass #-}
23
{-# LANGUAGE DeriveGeneric #-}
34
{-# LANGUAGE FlexibleContexts #-}
@@ -44,6 +45,7 @@ import Ouroboros.Consensus.Storage.LedgerDB.V1.DiffSeq
4445
)
4546
import qualified Ouroboros.Consensus.Storage.LedgerDB.V1.DiffSeq as DS
4647
import Ouroboros.Consensus.Storage.LedgerDB.V1.Lock
48+
import Ouroboros.Consensus.Util
4749
import Ouroboros.Consensus.Util.IOLike
4850

4951
{-------------------------------------------------------------------------------
@@ -90,9 +92,17 @@ deriving instance
9092
Close
9193
-------------------------------------------------------------------------------}
9294

93-
closeForkerEnv :: IOLike m => ForkerEnv m l blk -> m ()
94-
closeForkerEnv ForkerEnv{foeBackingStoreValueHandle} = do
95-
either (\(l, _, _) -> atomically . unsafeReleaseReadAccess $ l) (Monad.void . release . fst)
95+
closeForkerEnv :: IOLike m => Flag "ClosingAllForkers" -> ForkerEnv m l blk -> m ()
96+
closeForkerEnv closingAll ForkerEnv{foeBackingStoreValueHandle} = do
97+
either
98+
(\(l, _, _) -> atomically . unsafeReleaseReadAccess $ l)
99+
( Monad.void
100+
-- During shutdown, we will close all forkers from the main thread
101+
-- instead of from the thread that opened the forker, hence we need
102+
-- unsafeRelease.
103+
. (if getFlag closingAll then unsafeRelease else release)
104+
. fst
105+
)
96106
=<< takeMVar foeBackingStoreValueHandle
97107

98108
{-------------------------------------------------------------------------------

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -662,14 +662,20 @@ closeAllForkers ::
662662
m ()
663663
closeAllForkers lock forkersVar = do
664664
toClose <- atomically $ swapTVar forkersVar Map.empty
665-
mapM_ (closeForkerEnv lock) toClose
665+
mapM_ (closeForkerEnv (Flag @"ClosingAllForkers" True) lock) toClose
666666

667-
closeForkerEnv :: IOLike m => RAWLock m LDBLock -> ForkerEnv m l blk -> m ()
668-
closeForkerEnv lock frkEnv =
667+
closeForkerEnv ::
668+
IOLike m => Flag "ClosingAllForkers" -> RAWLock m LDBLock -> ForkerEnv m l blk -> m ()
669+
closeForkerEnv closingAll lock frkEnv =
669670
RAWLock.withWriteAccess lock $
670671
const $ do
671672
id =<< (atomically $ swapTVar (snd $ foeResourcesToRelease frkEnv) (pure ()))
672-
_ <- release (fst (foeResourcesToRelease frkEnv))
673+
_ <-
674+
-- During shutdown, we will close all forkers from the main thread
675+
-- instead of from the thread that opened the forker, hence we need
676+
-- unsafeRelease.
677+
(if getFlag closingAll then unsafeRelease else release)
678+
(fst (foeResourcesToRelease frkEnv))
673679
pure ((), LDBLock)
674680

675681
getForkerEnv ::
@@ -734,7 +740,7 @@ implForkerClose (LDBHandle varState) forkerKey = do
734740
<$> stateTVar
735741
(ldbForkers ldbEnv)
736742
(Map.updateLookupWithKey (\_ _ -> Nothing) forkerKey)
737-
whenJust menv (uncurry closeForkerEnv)
743+
whenJust menv (uncurry (closeForkerEnv $ Flag @"ClosingAllForkers" False))
738744

739745
newForker ::
740746
( IOLike m

0 commit comments

Comments
 (0)