@@ -19,6 +19,7 @@ module Ouroboros.Consensus.Storage.LedgerDB.V2.Forker
1919  , module  Ouroboros.Consensus.Storage.LedgerDB.Forker 
2020  ) where 
2121
22+ import  qualified  Control.Monad  as  Monad 
2223import  Control.RAWLock  hiding  (read )
2324import  Control.ResourceRegistry 
2425import  Control.Tracer 
@@ -52,6 +53,9 @@ data ForkerEnv m l blk = ForkerEnv
5253  --  ^  Config 
5354  , foeResourcesToRelease  ::  ! (RAWLock  m  () , ResourceKey  m , StrictTVar  m  (m  () ))
5455  --  ^  Release the resources 
56+   , foeInitialHandleKey  ::  ! (ResourceKey  m )
57+   --  ^  Resource key for the initial handle to ensure it is released. See 
58+   --  comments in 'implForkerCommit'.
5559  } 
5660  deriving  Generic 
5761
@@ -160,19 +164,12 @@ implForkerCommit env = do
160164                  AS. Empty  _ ->  pure  () 
161165                  _ AS. :<  closeOld' ->  closeLedgerSeq (LedgerSeq  closeOld')
162166                --  Finally, close the anchor of @lseq@ (which is a duplicate of
163-                 --  the head of @olddb'@).
164-                 -- 
165-                 --  Note if the resource registry used to create the Forker is
166-                 --  ephemeral as the one created on each Chain selection or each
167-                 --  Forging loop iteration, this first duplicated state will be
168-                 --  closed by the resource registry closing down, so this will be
169-                 --  a double release, which is fine. We prefer keeping this
170-                 --  action just in case some client passes a registry that
171-                 --  outlives the forker.
172-                 -- 
173-                 --  The rest of the states in the forker will be closed via
174-                 --  @foeResourcesToRelease@ instead of via the registry.
175-                 close $  tables $  AS. anchor lseq
167+                 --  the head of @olddb'@). To close this handle, we have to
168+                 --  release the 'foeInitialHandleKey' as that one is registered
169+                 --  on the registry used to open the forker. Releasing it will
170+                 --  call 'close' on the handle which will call 'release' on the key
171+                 --  for the handle.
172+                 Monad. void $  release foeInitialHandleKey
176173          pure  (closeDiscarded, LedgerSeq  newdb)
177174      )
178175
@@ -186,6 +183,7 @@ implForkerCommit env = do
186183    { foeLedgerSeq
187184    , foeSwitchVar
188185    , foeResourcesToRelease
186+     , foeInitialHandleKey
189187    } =  env
190188
191189  theImpossible = 
0 commit comments