@@ -72,6 +72,7 @@ import Ouroboros.Consensus.Util hiding (Some)
72
72
import Ouroboros.Consensus.Util.Args
73
73
import Ouroboros.Consensus.Util.IOLike
74
74
import qualified Ouroboros.Network.AnchoredSeq as AS
75
+ import Ouroboros.Network.Protocol.LocalStateQuery.Type
75
76
import qualified System.Directory as Dir
76
77
import System.FS.API
77
78
import qualified System.FS.IO as FSIO
@@ -114,9 +115,11 @@ prop_sequential ::
114
115
QC. Property
115
116
prop_sequential maxSuccess mkTestArguments getLmdbDir fsOps as = QC. withMaxSuccess maxSuccess $
116
117
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
119
121
checkNoLeakedHandles env
122
+ lift $ closeRegistry reg
120
123
QC. run $ closeLedgerDB testInternals >> clean
121
124
QC. assert True
122
125
@@ -129,9 +132,10 @@ initialEnvironment ::
129
132
IO (SomeHasFS IO , IO () ) ->
130
133
IO (FilePath , IO () ) ->
131
134
(SecurityParam -> FilePath -> TestArguments IO ) ->
135
+ ResourceRegistry IO ->
132
136
ChainDB IO ->
133
137
IO Environment
134
- initialEnvironment fsOps getLmdbDir mkTestArguments cdb = do
138
+ initialEnvironment fsOps getLmdbDir mkTestArguments reg cdb = do
135
139
(sfs, cleanupFS) <- fsOps
136
140
(lmdbDir, cleanupLMDB) <- getLmdbDir
137
141
pure $
@@ -143,6 +147,7 @@ initialEnvironment fsOps getLmdbDir mkTestArguments cdb = do
143
147
sfs
144
148
(pure $ NumOpenHandles 0 )
145
149
(cleanupFS >> cleanupLMDB)
150
+ reg
146
151
147
152
{- ------------------------------------------------------------------------------
148
153
Arguments
@@ -280,6 +285,9 @@ instance StateModel Model where
280
285
Action Model (ExtLedgerState TestBlock EmptyMK , ExtLedgerState TestBlock EmptyMK )
281
286
Init :: SecurityParam -> Action Model ()
282
287
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 ()
283
291
284
292
actionName WipeLedgerDB {} = " WipeLedgerDB"
285
293
actionName TruncateSnapshots {} = " TruncateSnapshots"
@@ -288,6 +296,7 @@ instance StateModel Model where
288
296
actionName GetState {} = " GetState"
289
297
actionName Init {} = " Init"
290
298
actionName ValidateAndCommit {} = " ValidateAndCommit"
299
+ actionName OpenAndCloseForker = " OpenAndCloseForker"
291
300
292
301
arbitraryAction _ UnInit = Some . Init <$> QC. arbitrary
293
302
arbitraryAction _ model@ (Model chain secParam) =
@@ -316,6 +325,7 @@ instance StateModel Model where
316
325
)
317
326
, (1 , pure $ Some WipeLedgerDB )
318
327
, (1 , pure $ Some TruncateSnapshots )
328
+ , (1 , pure $ Some OpenAndCloseForker )
319
329
]
320
330
321
331
initialState = UnInit
@@ -357,6 +367,7 @@ instance StateModel Model where
357
367
nextState state WipeLedgerDB _var = state
358
368
nextState state TruncateSnapshots _var = state
359
369
nextState state (DropAndRestore n) _var = modelRollback n state
370
+ nextState state OpenAndCloseForker _var = state
360
371
nextState UnInit _ _ = error " Uninitialized model created a command different than Init"
361
372
362
373
precondition UnInit Init {} = True
@@ -526,25 +537,26 @@ data Environment
526
537
(SomeHasFS IO )
527
538
(IO NumOpenHandles )
528
539
(IO () )
540
+ (ResourceRegistry IO )
529
541
530
542
instance RunModel Model (StateT Environment IO ) where
531
543
perform _ (Init secParam) _ = do
532
- Environment _ _ chainDb mkArgs fs _ cleanup <- get
544
+ Environment _ _ chainDb mkArgs fs _ cleanup rr <- get
533
545
(ldb, testInternals, getNumOpenHandles) <- lift $ do
534
546
let args = mkArgs secParam
535
547
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 )
537
549
perform _ WipeLedgerDB _ = do
538
- Environment _ testInternals _ _ _ _ _ <- get
550
+ Environment _ testInternals _ _ _ _ _ _ <- get
539
551
lift $ wipeLedgerDB testInternals
540
552
perform _ GetState _ = do
541
- Environment ldb _ _ _ _ _ _ <- get
553
+ Environment ldb _ _ _ _ _ _ _ <- get
542
554
lift $ atomically $ (,) <$> getImmutableTip ldb <*> getVolatileTip ldb
543
555
perform _ ForceTakeSnapshot _ = do
544
- Environment _ testInternals _ _ _ _ _ <- get
556
+ Environment _ testInternals _ _ _ _ _ _ <- get
545
557
lift $ takeSnapshotNOW testInternals TakeAtImmutableTip Nothing
546
558
perform _ (ValidateAndCommit n blks) _ = do
547
- Environment ldb _ chainDb _ _ _ _ <- get
559
+ Environment ldb _ chainDb _ _ _ _ _ <- get
548
560
lift $ do
549
561
atomically $
550
562
modifyTVar (dbBlocks chainDb) $
@@ -561,13 +573,20 @@ instance RunModel Model (StateT Environment IO) where
561
573
ValidateExceededRollBack {} -> error " Unexpected Rollback"
562
574
ValidateLedgerError (AnnLedgerError forker _ _) -> forkerClose forker >> error " Unexpected ledger error"
563
575
perform state@ (Model _ secParam) (DropAndRestore n) lk = do
564
- Environment _ testInternals chainDb _ _ _ _ <- get
576
+ Environment _ testInternals chainDb _ _ _ _ _ <- get
565
577
lift $ do
566
578
atomically $ modifyTVar (dbChain chainDb) (drop (fromIntegral n))
567
579
closeLedgerDB testInternals
568
580
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
569
588
perform _ TruncateSnapshots _ = do
570
- Environment _ testInternals _ _ _ _ _ <- get
589
+ Environment _ testInternals _ _ _ _ _ _ <- get
571
590
lift $ truncateSnapshots testInternals
572
591
perform UnInit _ _ = error " Uninitialized model created a command different than Init"
573
592
@@ -622,7 +641,7 @@ mkTrackOpenHandles = do
622
641
623
642
-- | Check that we didn't leak any 'LedgerTablesHandle's (with V2 only).
624
643
checkNoLeakedHandles :: Environment -> QC. PropertyM IO ()
625
- checkNoLeakedHandles (Environment _ testInternals _ _ _ getNumOpenHandles _) = do
644
+ checkNoLeakedHandles (Environment _ testInternals _ _ _ getNumOpenHandles _ _ ) = do
626
645
expected <- liftIO $ NumOpenHandles <$> LedgerDB. getNumLedgerTablesHandles testInternals
627
646
actual <- liftIO getNumOpenHandles
628
647
QC. assertWith (actual == expected) $
0 commit comments