Gabriella439 / Haskell-Pipes-Safe-Library

Safety for the pipes ecosystem
BSD 3-Clause "New" or "Revised" License
26 stars 21 forks source link

Make explicit buffering easier when using `withFile` #30

Open 3noch opened 7 years ago

3noch commented 7 years ago

I had a bug where my output file was always empty. I tracked it down to the default buffering mode. I think it would be better offer a variant of withFile that explicitly specifies the buffering mode.

What do you think?

withFile' :: MonadSafe m => FilePath -> IO.IOMode -> IO.BufferMode -> (IO.Handle -> m r) -> m r
withFile' file ioMode bufferMode = PS.bracket
  (liftIO $ do
    h <- IO.openFile file ioMode
    IO.hSetBuffering h bufferMode
    pure h)
  (liftIO . IO.hClose)
Gabriella439 commented 7 years ago

System.IO.hClose should ensure that all buffers are flushed, regardless of the buffering mode used. How were you acquiring and releasing the Handle?

3noch commented 7 years ago

I was using withFile.

3noch commented 7 years ago

In that case, this may be a more sinister bug than I thought. I was using Ctrl+C to stop the long-running process. I wonder if the async exception didn't get handled appropriately.

Gabriella439 commented 7 years ago

Yeah, this sounds like a bug in pipes-safe exception handling if it's not finalizing the Handle. I will investigate, although it might take me a few days to get to this

Gabriella439 commented 7 years ago

Also, in the meantime, do you happen to have a reproducing test case for your problem that I can use?

3noch commented 7 years ago

I'll try to get one to you.