@@ -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
@@ -280,6 +281,9 @@ instance StateModel Model where
280281 Action Model (ExtLedgerState TestBlock EmptyMK , ExtLedgerState TestBlock EmptyMK )
281282 Init :: SecurityParam -> Action Model ()
282283 ValidateAndCommit :: Word64 -> [TestBlock ] -> Action Model ()
284+ -- \| This action is used only to observe the side effects of closing an
285+ -- uncommitted forker, to ensure all handles are properly deallocated.
286+ OpenAndCloseForker :: Action Model ()
283287
284288 actionName WipeLedgerDB {} = " WipeLedgerDB"
285289 actionName TruncateSnapshots {} = " TruncateSnapshots"
@@ -288,6 +292,7 @@ instance StateModel Model where
288292 actionName GetState {} = " GetState"
289293 actionName Init {} = " Init"
290294 actionName ValidateAndCommit {} = " ValidateAndCommit"
295+ actionName OpenAndCloseForker = " OpenAndCloseForker"
291296
292297 arbitraryAction _ UnInit = Some . Init <$> QC. arbitrary
293298 arbitraryAction _ model@ (Model chain secParam) =
@@ -322,6 +327,7 @@ instance StateModel Model where
322327 )
323328 , (1 , pure $ Some WipeLedgerDB )
324329 , (1 , pure $ Some TruncateSnapshots )
330+ , (1 , pure $ Some OpenAndCloseForker )
325331 ]
326332
327333 initialState = UnInit
@@ -363,6 +369,7 @@ instance StateModel Model where
363369 nextState state WipeLedgerDB _var = state
364370 nextState state TruncateSnapshots _var = state
365371 nextState state (DropAndRestore n) _var = modelRollback n state
372+ nextState state OpenAndCloseForker _var = state
366373 nextState UnInit _ _ = error " Uninitialized model created a command different than Init"
367374
368375 precondition UnInit Init {} = True
@@ -583,6 +590,14 @@ instance RunModel Model (StateT Environment IO) where
583590 atomically $ modifyTVar (dbChain chainDb) (drop (fromIntegral n))
584591 closeLedgerDB testInternals
585592 perform state (Init secParam) lk
593+ perform _ OpenAndCloseForker _ = do
594+ Environment ldb _ _ _ _ _ _ <- get
595+ lift $ withRegistry $ \ rr -> do
596+ eFrk <- LedgerDB. getForkerAtTarget ldb rr VolatileTip
597+ case eFrk of
598+ Left err -> error $ " Impossible: can't acquire forker at tip: " <> show err
599+ Right frk -> forkerClose frk
600+ pure $ pure ()
586601 perform _ TruncateSnapshots _ = do
587602 Environment _ testInternals _ _ _ _ _ <- get
588603 lift $ truncateSnapshots testInternals
0 commit comments