Skip to content

Commit fbb13ee

Browse files
committed
Don't forget to release HasBlockIO in the public API
There are a few places where functions in the public API create a `HasBlockIO` for the user, but since #742 when a session is closed the `HasBlockIO` it contains does not get closed automatically anymore. So in the public API, we have to be extra careful to do so when the public API opens a `HasBlockIO`.
1 parent 9f6b36f commit fbb13ee

File tree

2 files changed

+22
-22
lines changed

2 files changed

+22
-22
lines changed

src/Database/LSMTree.hs

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ module Database.LSMTree (
2424
withNewSession,
2525
withRestoreSession,
2626
openSession,
27-
openSessionIO,
2827
newSession,
2928
restoreSession,
3029
closeSession,
@@ -275,7 +274,7 @@ import qualified Database.LSMTree.Internal.Unsafe as Internal
275274
import Prelude hiding (lookup, take, takeWhile)
276275
import System.FS.API (FsPath, HasFS (..), MountPoint (..), mkFsPath)
277276
import System.FS.BlockIO.API (HasBlockIO (..), defaultIOCtxParams)
278-
import System.FS.BlockIO.IO (ioHasBlockIO, withIOHasBlockIO)
277+
import System.FS.BlockIO.IO (withIOHasBlockIO)
279278
import System.FS.IO (HandleIO, ioHasFS)
280279
import System.Random (randomIO)
281280

@@ -622,22 +621,6 @@ openSession ::
622621
openSession tracer hasFS hasBlockIO sessionSalt sessionDir =
623622
Session <$> Internal.openSession tracer hasFS hasBlockIO sessionSalt sessionDir
624623

625-
-- | Variant of 'openSession' that is specialised to 'IO' using the real filesystem.
626-
openSessionIO ::
627-
Tracer IO LSMTreeTrace ->
628-
-- | The session directory.
629-
FilePath ->
630-
IO (Session IO)
631-
openSessionIO tracer sessionDir = do
632-
let mountPoint = MountPoint sessionDir
633-
let sessionDirFsPath = mkFsPath []
634-
let hasFS = ioHasFS mountPoint
635-
sessionSalt <- randomIO
636-
let acquireHasBlockIO = ioHasBlockIO hasFS defaultIOCtxParams
637-
let releaseHasBlockIO HasBlockIO{close} = close
638-
bracketOnError acquireHasBlockIO releaseHasBlockIO $ \hasBlockIO ->
639-
openSession tracer hasFS hasBlockIO sessionSalt sessionDirFsPath
640-
641624
{- |
642625
Create a new session.
643626

src/Database/LSMTree/Simple.hs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,8 @@ module Database.LSMTree.Simple (
157157
) where
158158

159159
import Control.ActionRegistry (mapExceptionWithActionRegistry)
160-
import Control.Exception.Base (Exception, SomeException (..))
160+
import Control.Exception (Exception, SomeException (..),
161+
bracketOnError, finally)
161162
import Data.Bifunctor (Bifunctor (..))
162163
import Data.Coerce (coerce)
163164
import Data.Kind (Type)
@@ -185,7 +186,14 @@ import Database.LSMTree (BloomFilterAlloc, CursorClosedError (..),
185186
serialiseValueIdentity, serialiseValueIdentityUpToSlicing,
186187
toSnapshotName)
187188
import qualified Database.LSMTree as LSMT
189+
import qualified Database.LSMTree.Internal.Types as LSMT
190+
import qualified Database.LSMTree.Internal.Unsafe as Internal
188191
import Prelude hiding (lookup, take, takeWhile)
192+
import System.FS.API (MountPoint (..), mkFsPath)
193+
import System.FS.BlockIO.API (HasBlockIO (..), defaultIOCtxParams)
194+
import System.FS.BlockIO.IO (ioHasBlockIO)
195+
import System.FS.IO (ioHasFS)
196+
import System.Random (randomIO)
189197

190198
--------------------------------------------------------------------------------
191199
-- Example
@@ -450,7 +458,14 @@ openSession ::
450458
openSession dir = do
451459
let tracer = mempty
452460
_convertSessionDirErrors dir $ do
453-
Session <$> LSMT.openSessionIO tracer dir
461+
let mountPoint = MountPoint dir
462+
let sessionDirFsPath = mkFsPath []
463+
let hasFS = ioHasFS mountPoint
464+
sessionSalt <- randomIO
465+
let acquireHasBlockIO = ioHasBlockIO hasFS defaultIOCtxParams
466+
let releaseHasBlockIO HasBlockIO{close} = close
467+
bracketOnError acquireHasBlockIO releaseHasBlockIO $ \hasBlockIO ->
468+
Session <$> LSMT.openSession tracer hasFS hasBlockIO sessionSalt sessionDirFsPath
454469

455470
{- |
456471
Close a session.
@@ -471,8 +486,10 @@ All other operations on a closed session will throw an exception.
471486
closeSession ::
472487
Session ->
473488
IO ()
474-
closeSession (Session session) =
475-
LSMT.closeSession session
489+
closeSession (Session session@(LSMT.Session session')) = do
490+
HasBlockIO{close} <- Internal.withKeepSessionOpen session' $
491+
pure . Internal.sessionHasBlockIO
492+
LSMT.closeSession session `finally` close
476493

477494
--------------------------------------------------------------------------------
478495
-- Tables

0 commit comments

Comments
 (0)