Skip to content

Commit efb5546

Browse files
committed
Merge pull request acid-state#58 from sdx23/master
fix blocked in mvar when running out of disk space
2 parents 61003d1 + 03110cb commit efb5546

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

src/Data/Acid/Log.hs

+9-6
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import Text.Printf ( printf )
4343

4444
import Paths_acid_state ( version )
4545
import Data.Version ( showVersion )
46+
import Control.Exception ( handle, IOException )
4647

4748
type EntryId = Int
4849

@@ -87,7 +88,8 @@ openFileLog identifier = do
8788
currentState <- newEmptyMVar
8889
queue <- newTVarIO ([], [])
8990
nextEntryRef <- newTVarIO 0
90-
tid2 <- forkIO $ fileWriter currentState queue
91+
tid1 <- myThreadId
92+
tid2 <- forkIO $ fileWriter currentState queue tid1
9193
let fLog = FileLog { logIdentifier = identifier
9294
, logCurrent = currentState
9395
, logNextEntryId = nextEntryRef
@@ -105,16 +107,17 @@ openFileLog identifier = do
105107
putMVar currentState handle
106108
return fLog
107109

108-
fileWriter :: MVar FHandle -> TVar ([Lazy.ByteString], [IO ()]) -> IO ()
109-
fileWriter currentState queue = forever $ do
110+
fileWriter :: MVar FHandle -> TVar ([Lazy.ByteString], [IO ()]) -> ThreadId -> IO ()
111+
fileWriter currentState queue parentTid = forever $ do
110112
(entries, actions) <- atomically $ do
111113
(entries, actions) <- readTVar queue
112114
when (null entries && null actions) retry
113115
writeTVar queue ([], [])
114116
return (reverse entries, reverse actions)
115-
withMVar currentState $ \fd -> do
116-
let arch = Archive.packEntries entries
117-
writeToDisk fd (repack arch)
117+
handle (\e -> throwTo parentTid (e :: IOException)) $
118+
withMVar currentState $ \fd -> do
119+
let arch = Archive.packEntries entries
120+
writeToDisk fd (repack arch)
118121
sequence_ actions
119122
yield
120123

0 commit comments

Comments
 (0)