@@ -72,6 +72,7 @@ import Ouroboros.Consensus.Util hiding (Some)
7272import Ouroboros.Consensus.Util.Args
7373import Ouroboros.Consensus.Util.IOLike
7474import qualified Ouroboros.Network.AnchoredSeq as AS
75+ import Ouroboros.Network.Protocol.LocalStateQuery.Type
7576import qualified System.Directory as Dir
7677import System.FS.API
7778import qualified System.FS.IO as FSIO
@@ -114,9 +115,11 @@ prop_sequential ::
114115 QC. Property
115116prop_sequential maxSuccess mkTestArguments getLmdbDir fsOps as = QC. withMaxSuccess maxSuccess $
116117 QC. monadicIO $ do
117- ref <- lift $ initialEnvironment fsOps getLmdbDir mkTestArguments =<< initChainDB
118- (_, env@ (Environment _ testInternals _ _ _ _ clean)) <- runPropertyStateT (runActions as) ref
118+ reg <- lift $ unsafeNewRegistry
119+ ref <- lift $ initialEnvironment fsOps getLmdbDir mkTestArguments reg =<< initChainDB
120+ (_, env@ (Environment _ testInternals _ _ _ _ clean _)) <- runPropertyStateT (runActions as) ref
119121 checkNoLeakedHandles env
122+ lift $ closeRegistry reg
120123 QC. run $ closeLedgerDB testInternals >> clean
121124 QC. assert True
122125
@@ -129,9 +132,10 @@ initialEnvironment ::
129132 IO (SomeHasFS IO , IO () ) ->
130133 IO (FilePath , IO () ) ->
131134 (SecurityParam -> FilePath -> TestArguments IO ) ->
135+ ResourceRegistry IO ->
132136 ChainDB IO ->
133137 IO Environment
134- initialEnvironment fsOps getLmdbDir mkTestArguments cdb = do
138+ initialEnvironment fsOps getLmdbDir mkTestArguments reg cdb = do
135139 (sfs, cleanupFS) <- fsOps
136140 (lmdbDir, cleanupLMDB) <- getLmdbDir
137141 pure $
@@ -143,6 +147,7 @@ initialEnvironment fsOps getLmdbDir mkTestArguments cdb = do
143147 sfs
144148 (pure $ NumOpenHandles 0 )
145149 (cleanupFS >> cleanupLMDB)
150+ reg
146151
147152{- ------------------------------------------------------------------------------
148153 Arguments
@@ -280,6 +285,9 @@ instance StateModel Model where
280285 Action Model (ExtLedgerState TestBlock EmptyMK , ExtLedgerState TestBlock EmptyMK )
281286 Init :: SecurityParam -> Action Model ()
282287 ValidateAndCommit :: Word64 -> [TestBlock ] -> Action Model ()
288+ -- | This action is used only to observe the side effects of closing an
289+ -- uncommitted forker, to ensure all handles are properly deallocated.
290+ OpenAndCloseForker :: Action Model ()
283291
284292 actionName WipeLedgerDB {} = " WipeLedgerDB"
285293 actionName TruncateSnapshots {} = " TruncateSnapshots"
@@ -288,6 +296,7 @@ instance StateModel Model where
288296 actionName GetState {} = " GetState"
289297 actionName Init {} = " Init"
290298 actionName ValidateAndCommit {} = " ValidateAndCommit"
299+ actionName OpenAndCloseForker = " OpenAndCloseForker"
291300
292301 arbitraryAction _ UnInit = Some . Init <$> QC. arbitrary
293302 arbitraryAction _ model@ (Model chain secParam) =
@@ -316,6 +325,7 @@ instance StateModel Model where
316325 )
317326 , (1 , pure $ Some WipeLedgerDB )
318327 , (1 , pure $ Some TruncateSnapshots )
328+ , (1 , pure $ Some OpenAndCloseForker )
319329 ]
320330
321331 initialState = UnInit
@@ -357,6 +367,7 @@ instance StateModel Model where
357367 nextState state WipeLedgerDB _var = state
358368 nextState state TruncateSnapshots _var = state
359369 nextState state (DropAndRestore n) _var = modelRollback n state
370+ nextState state OpenAndCloseForker _var = state
360371 nextState UnInit _ _ = error " Uninitialized model created a command different than Init"
361372
362373 precondition UnInit Init {} = True
@@ -526,25 +537,26 @@ data Environment
526537 (SomeHasFS IO )
527538 (IO NumOpenHandles )
528539 (IO () )
540+ (ResourceRegistry IO )
529541
530542instance RunModel Model (StateT Environment IO ) where
531543 perform _ (Init secParam) _ = do
532- Environment _ _ chainDb mkArgs fs _ cleanup <- get
544+ Environment _ _ chainDb mkArgs fs _ cleanup rr <- get
533545 (ldb, testInternals, getNumOpenHandles) <- lift $ do
534546 let args = mkArgs secParam
535547 openLedgerDB (argFlavorArgs args) chainDb (argLedgerDbCfg args) fs
536- put (Environment ldb testInternals chainDb mkArgs fs getNumOpenHandles cleanup)
548+ put (Environment ldb testInternals chainDb mkArgs fs getNumOpenHandles cleanup rr )
537549 perform _ WipeLedgerDB _ = do
538- Environment _ testInternals _ _ _ _ _ <- get
550+ Environment _ testInternals _ _ _ _ _ _ <- get
539551 lift $ wipeLedgerDB testInternals
540552 perform _ GetState _ = do
541- Environment ldb _ _ _ _ _ _ <- get
553+ Environment ldb _ _ _ _ _ _ _ <- get
542554 lift $ atomically $ (,) <$> getImmutableTip ldb <*> getVolatileTip ldb
543555 perform _ ForceTakeSnapshot _ = do
544- Environment _ testInternals _ _ _ _ _ <- get
556+ Environment _ testInternals _ _ _ _ _ _ <- get
545557 lift $ takeSnapshotNOW testInternals TakeAtImmutableTip Nothing
546558 perform _ (ValidateAndCommit n blks) _ = do
547- Environment ldb _ chainDb _ _ _ _ <- get
559+ Environment ldb _ chainDb _ _ _ _ _ <- get
548560 lift $ do
549561 atomically $
550562 modifyTVar (dbBlocks chainDb) $
@@ -561,13 +573,20 @@ instance RunModel Model (StateT Environment IO) where
561573 ValidateExceededRollBack {} -> error " Unexpected Rollback"
562574 ValidateLedgerError (AnnLedgerError forker _ _) -> forkerClose forker >> error " Unexpected ledger error"
563575 perform state@ (Model _ secParam) (DropAndRestore n) lk = do
564- Environment _ testInternals chainDb _ _ _ _ <- get
576+ Environment _ testInternals chainDb _ _ _ _ _ <- get
565577 lift $ do
566578 atomically $ modifyTVar (dbChain chainDb) (drop (fromIntegral n))
567579 closeLedgerDB testInternals
568580 perform state (Init secParam) lk
581+ perform _ OpenAndCloseForker _ = do
582+ Environment ldb _ _ _ _ _ _ _ <- get
583+ lift $ withRegistry $ \ rr -> do
584+ eFrk <- LedgerDB. getForkerAtTarget ldb rr VolatileTip
585+ case eFrk of
586+ Left err -> error $ " Impossible: can't acquire forker at tip: " <> show err
587+ Right frk -> forkerClose frk
569588 perform _ TruncateSnapshots _ = do
570- Environment _ testInternals _ _ _ _ _ <- get
589+ Environment _ testInternals _ _ _ _ _ _ <- get
571590 lift $ truncateSnapshots testInternals
572591 perform UnInit _ _ = error " Uninitialized model created a command different than Init"
573592
@@ -622,7 +641,7 @@ mkTrackOpenHandles = do
622641
623642-- | Check that we didn't leak any 'LedgerTablesHandle's (with V2 only).
624643checkNoLeakedHandles :: Environment -> QC. PropertyM IO ()
625- checkNoLeakedHandles (Environment _ testInternals _ _ _ getNumOpenHandles _) = do
644+ checkNoLeakedHandles (Environment _ testInternals _ _ _ getNumOpenHandles _ _ ) = do
626645 expected <- liftIO $ NumOpenHandles <$> LedgerDB. getNumLedgerTablesHandles testInternals
627646 actual <- liftIO getNumOpenHandles
628647 QC. assertWith (actual == expected) $
0 commit comments