Skip to content

Commit 13dbc27

Browse files
committed
Release the forker initial key
1 parent 4a1ea5f commit 13dbc27

File tree

3 files changed

+36
-13
lines changed

3 files changed

+36
-13
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<!--
2+
A new scriv changelog fragment.
3+
4+
Uncomment the section that is right (remove the HTML comment wrapper).
5+
For top level release notes, leave all the headers commented out.
6+
-->
7+
8+
### Patch
9+
10+
- Ensure the initial handle allocated by opening a forker is deallocated in all
11+
situations.
12+
13+
<!--
14+
### Non-Breaking
15+
16+
- A bullet item for the Non-Breaking category.
17+
18+
-->
19+
<!--
20+
### Breaking
21+
22+
- A bullet item for the Breaking category.
23+
24+
-->

ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Storage/LedgerDB/V2.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -781,6 +781,7 @@ newForker h ldbEnv rr (st, rk) = do
781781
, foeSwitchVar = ldbSeq ldbEnv
782782
, foeTracer = tr
783783
, foeResourcesToRelease = (ldbOpenHandlesLock ldbEnv, k, toRelease)
784+
, foeInitialHandleKey = rk
784785
}
785786
atomically $ modifyTVar (ldbForkers ldbEnv) $ Map.insert forkerKey forkerEnv
786787
pure $

ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Storage/LedgerDB/V2/Forker.hs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -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
2223
import Control.RAWLock hiding (read)
2324
import Control.ResourceRegistry
2425
import 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 in the 'lgrRegistry'.
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

Comments
 (0)