@@ -45,7 +45,7 @@ import Cardano.Slotting.Slot (EpochNo (..))
4545
4646import qualified Cardano.Db as DB
4747import 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 )
4949import Cardano.DbSync.Config (configureLogging )
5050import Cardano.DbSync.Config.Cardano
5151import Cardano.DbSync.Config.Types
@@ -54,6 +54,7 @@ import Cardano.DbSync.DbEvent
5454import Cardano.DbSync.Era
5555import Cardano.DbSync.Error
5656import Cardano.DbSync.Ledger.State
57+ import Cardano.DbSync.Ledger.Types (HasLedgerEnv (.. ))
5758import Cardano.DbSync.OffChain (runFetchOffChainPoolThread , runFetchOffChainVoteThread )
5859import Cardano.DbSync.Rollback (handlePostRollbackSnapshots , unsafeRollback )
5960import Cardano.DbSync.Sync (runSyncNodeClient )
@@ -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 ()
0 commit comments