Skip to content

Commit

Permalink
FIXUP: ref count cursor blobs properly
Browse files Browse the repository at this point in the history
  • Loading branch information
dcoutts committed Sep 18, 2024
1 parent d68afd9 commit 8f1fa56
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/Database/LSMTree/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,10 @@ data CursorEnv m h = CursorEnv {
-- | The runs held open by the cursor. We must remove a reference when the
-- cursor gets closed.
, cursorRuns :: !(V.Vector (Run m (Handle h)))

-- | The write buffer blobs, which like the runs, we have to keep open
-- untile the cursor is closed.
, cursorWBB :: WBB.WriteBufferBlobs m h
}

{-# SPECIALISE withCursor :: OffsetKey -> TableHandle IO h -> (Cursor IO h -> IO a) -> IO a #-}
Expand Down Expand Up @@ -859,12 +863,13 @@ newCursor !offsetKey th = withOpenTable th $ \thEnv -> do
-- 'sessionOpenTables'.
withOpenSession cursorSession $ \_ -> do
withTempRegistry $ \reg -> do
(writeBuffer, cursorRuns) <-
(writeBuffer, writeBufferBlobs, cursorRuns) <-
allocTableContent reg (tableContent thEnv)
cursorReaders <-
allocateMaybeTemp reg
(Readers.new hfs hbio offsetKey (Just writeBuffer) cursorRuns)
(Readers.close hfs hbio)
let cursorWBB = writeBufferBlobs
cursorState <- newMVar (CursorOpen CursorEnv {..})
let !cursor = Cursor {cursorState, cursorTracer}
-- Track cursor, but careful: If now an exception is raised, all
Expand All @@ -891,7 +896,7 @@ newCursor !offsetKey th = withOpenTable th $ \thEnv -> do
allocateTemp reg
(Run.addReference r)
(\_ -> Run.removeReference r)
pure (wb, runs)
pure (wb, wbblobs, runs)

{-# SPECIALISE closeCursor :: Cursor IO h -> IO () #-}
-- | See 'Database.LSMTree.Normal.closeCursor'.
Expand All @@ -917,6 +922,7 @@ closeCursor Cursor {..} = do

forM_ cursorReaders $ freeTemp reg . Readers.close hfs hbio
V.forM_ cursorRuns $ freeTemp reg . Run.removeReference
freeTemp reg (WBB.removeReference cursorWBB)
return CursorClosed

{-# SPECIALISE readCursor :: ResolveSerialisedValue -> Int -> Cursor IO h -> (SerialisedKey -> SerialisedValue -> Maybe (WeakBlobRef IO (Handle h)) -> res) -> IO (V.Vector res) #-}
Expand Down

0 comments on commit 8f1fa56

Please sign in to comment.