Skip to content

Commit 4fb3784

Browse files
committed
Test that uncommitted forkers do not leak handles
1 parent df88019 commit 4fb3784

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

ouroboros-consensus/ouroboros-consensus.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,7 @@ test-suite storage-test
751751
ouroboros-consensus,
752752
ouroboros-network-api,
753753
ouroboros-network-mock,
754+
ouroboros-network-protocols,
754755
pretty-show,
755756
quickcheck-dynamic,
756757
quickcheck-lockstep ^>=0.8,

ouroboros-consensus/test/storage-test/Test/Ouroboros/Storage/LedgerDB/StateMachine.hs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ import Ouroboros.Consensus.Util hiding (Some)
7272
import Ouroboros.Consensus.Util.Args
7373
import Ouroboros.Consensus.Util.IOLike
7474
import qualified Ouroboros.Network.AnchoredSeq as AS
75+
import Ouroboros.Network.Protocol.LocalStateQuery.Type
7576
import qualified System.Directory as Dir
7677
import System.FS.API
7778
import 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

Comments
 (0)