These are chat archives for vu3rdd/functorrent
Here is the problem: We might kill the thread half way during a write, corrupting the file.
Previous solution: Wait for threads to complete themselves. The thread goes back into main loop after it finishes writing. It gets a new command and if that is STOP, it goes down
Issue with prev solution: pkill will corrupt the file. So will a bad exception or something we cant anticipate today
Current solution: Make threads
kill -safe. Let them be killed at any time and let it manage cleanup internally.
Only remaining question, how to make writeFile work nicely with async exceptions?
I'll go figure that out.
-- | Write a 'ByteString' to a file. writeFile :: FilePath -> ByteString -> IO () writeFile f txt = bracket (openBinaryFile f WriteMode) hClose (\h -> hPut h txt)
=> openFile' is also interruptible
openBinaryFile :: FilePath -> IOMode -> IO Handle openBinaryFile fp m = catchException (openFile' fp m True True) (\e -> ioError (addFilePathToIOError "openBinaryFile" fp e))
I cant think of anything other than using a writer thread and the same mechanism we used for logging. Also, replace both logging and file writer MVar with a Chanel (a mvar link list) so that read and write are decoupled and a slow disk wont slow down peer threads.
Channels over single mvar is explained well here http://chimera.labs.oreilly.com/books/1230000000929/ch07.html#sec_channels