Skip to content

Commit d1600de

Browse files
committed
Make sure we finally release Handles
This is necessery for tests that restart the DBSync thread For production the Handles were already closed on the OS level Signed-off-by: Kostas Dermentzis <kostas.dermentzis@iohk.io>
1 parent 5d057f5 commit d1600de

3 files changed

Lines changed: 30 additions & 4 deletions

File tree

cardano-db-sync/src/Cardano/DbSync.hs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ import Cardano.Slotting.Slot (EpochNo (..))
4545

4646
import qualified Cardano.Db as DB
4747
import Cardano.DbSync.Api
48-
import Cardano.DbSync.Api.Types (InsertOptions (..), RunMigration, SyncEnv (..), SyncOptions (..), envLedgerEnv)
48+
import Cardano.DbSync.Api.Types (InsertOptions (..), LedgerEnv (..), RunMigration, SyncEnv (..), SyncOptions (..), envLedgerEnv)
49+
import Cardano.DbSync.Ledger.Types (HasLedgerEnv (..))
4950
import Cardano.DbSync.Config (configureLogging)
5051
import Cardano.DbSync.Config.Cardano
5152
import Cardano.DbSync.Config.Types
@@ -248,6 +249,10 @@ runSyncNode metricsSetters trce iomgr dbConnSetting runNearTipMigrationFnc syncN
248249

249250
-- communication channel between datalayer thread and chainsync-client thread
250251
threadChannels <- liftIO newThreadChannels
252+
-- 'finally' on the worker pool ensures the LSM session (and any other
253+
-- backend resources) are closed even when db-sync is cancelled or
254+
-- crashes — important for tests that restart db-sync in the same
255+
-- process and need the OS file lock to be released.
251256
liftIO $
252257
mapConcurrently_
253258
id
@@ -257,6 +262,7 @@ runSyncNode metricsSetters trce iomgr dbConnSetting runNearTipMigrationFnc syncN
257262
, runFetchOffChainVoteThread syncEnv
258263
, runLedgerStateWriteThread (getTrace syncEnv) (envLedgerEnv syncEnv)
259264
]
265+
`finally` closeLedgerEnv syncEnv
260266
)
261267
where
262268
useShelleyInit :: SyncNodeConfig -> Bool
@@ -353,3 +359,14 @@ txOutConfigToTableType config = case config of
353359
TxOutConsumed _ (UseTxOutAddress flag) -> if flag then DB.TxOutVariantAddress else DB.TxOutVariantCore
354360
TxOutConsumedPrune _ (UseTxOutAddress flag) -> if flag then DB.TxOutVariantAddress else DB.TxOutVariantCore
355361
TxOutConsumedBootstrap _ (UseTxOutAddress flag) -> if flag then DB.TxOutVariantAddress else DB.TxOutVariantCore
362+
363+
-- | Release backend resources held by the ledger environment.
364+
-- Currently this closes the LSM session (no-op for InMemory and NoLedger).
365+
closeLedgerEnv :: SyncEnv -> IO ()
366+
closeLedgerEnv syncEnv = case envLedgerEnv syncEnv of
367+
HasLedger le -> do
368+
let trce = leTrace le
369+
logInfo trce "closeLedgerEnv: closing LSM session..."
370+
leClose le
371+
logInfo trce "closeLedgerEnv: closed."
372+
NoLedger _ -> pure ()

cardano-db-sync/src/Cardano/DbSync/Ledger/State.hs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ mkHasLedgerEnv trce protoInfo dir nw maxLovelaceSupply systemStart syncOptions b
206206
let codecConfig = configCodec $ Consensus.pInfoConfig protoInfo
207207
someHasFS = SomeHasFS $ ioHasFS (MountPoint $ unLedgerStateDir dir)
208208
snapTracer = Tracer.nullTracer -- TODO: wire up snapshot tracing
209-
(snapMgr, initGen, loadSnap) <- case backend of
209+
(snapMgr, initGen, loadSnap, closeBackend) <- case backend of
210210
LedgerBackendInMemory -> do
211211
res <-
212212
runWithTempRegistry $
@@ -223,7 +223,7 @@ mkHasLedgerEnv trce protoInfo dir nw maxLovelaceSupply systemStart syncOptions b
223223
case eResult of
224224
Left err -> pure $ Left $ textShow err
225225
Right (cRef, _pt) -> pure $ Right cRef
226-
pure (sm, ig, ld)
226+
pure (sm, ig, ld, pure ())
227227
LedgerBackendLSM mPath -> do
228228
let lsmPath = fromMaybe (unLedgerStateDir dir </> "lsm") mPath
229229
salt <- fst . genWord64 <$> newStdGen
@@ -243,7 +243,13 @@ mkHasLedgerEnv trce protoInfo dir nw maxLovelaceSupply systemStart syncOptions b
243243
case eResult of
244244
Left err -> pure $ Left $ textShow err
245245
Right (cRef, _pt) -> pure $ Right cRef
246-
pure (sm, ig, ld)
246+
-- Close the LSM session on shutdown to release the file lock.
247+
-- mkResources allocates the session via 'allocateTemp ... impossibleToNotTransfer',
248+
-- which means it is intentionally NOT closed when runWithTempRegistry exits
249+
-- (similar to bracketOnError). We release it explicitly here so the lock
250+
-- is freed even when the same process restarts db-sync (test scenario).
251+
let releaseLsm = releaseResources (Proxy @CardanoBlock) res
252+
pure (sm, ig, ld, releaseLsm)
247253

248254
pure
249255
HasLedgerEnv
@@ -264,6 +270,7 @@ mkHasLedgerEnv trce protoInfo dir nw maxLovelaceSupply systemStart syncOptions b
264270
, leSnapshotManager = snapMgr
265271
, leInitGenesis = initGen
266272
, leLoadSnapshot = loadSnap
273+
, leClose = closeBackend
267274
}
268275

269276
getTopLevelconfigHasLedger :: HasLedgerEnv -> TopLevelConfig CardanoBlock

cardano-db-sync/src/Cardano/DbSync/Ledger/Types.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ data HasLedgerEnv = HasLedgerEnv
8787
-- ^ Create the initial consensus StateRef from genesis
8888
, leLoadSnapshot :: !(DiskSnapshot -> IO (Either Text ConsensusStateRef))
8989
-- ^ Load a snapshot from disk using the appropriate backend
90+
, leClose :: !(IO ())
91+
-- ^ Release backend resources (LSM session, registry, etc.). Call on shutdown.
9092
}
9193

9294
-- | Pure ledger state, stored in LedgerDB checkpoints.

0 commit comments

Comments
 (0)