[Haskell-cafe] Why aren't files flushed at exit?

Felipe Almeida Lessa felipe.lessa at gmail.com
Sun Jul 17 16:10:05 CEST 2011


On Sun, Jul 17, 2011 at 10:41 AM, Paul Johnson <paul at cogito.org.uk> wrote:
> If you open a file for writing and then exit with output unflushed, then
> Haskell does not flush the file for you.  In ghci the program seems to work,
> but then when you compile it in ghc it mysteriously fails.
>
> I've just been bitten by this, but when I went to the bug tracker I found
> http://hackage.haskell.org/trac/ghc/ticket/4119 ticket 4119, which describes
> this behaviour and was resolved as "invalid".  So presumably this behaviour
> is by design.
>
> Given that most environments get this right, why doesn't Haskell?

If you are asking why finalizers are not guaranteed to run, I don't
really know the answer for sure.

But if you are asking why that bug was marked invalid, then that's
because it's good practice anyway to know the lifetime of you handles,
specially since they are a scarce resource.  If you need a handle for
the whole lifetime of your program, use withFile on the main function.
 If you don't need it for the whole lifetime, then you already should
be careful about not leaving it opened.  If your program is a
long-running process, maybe you should also hFlush at some points to
minimize damage on hardware failures and system reboots.

Cheers, =)

-- 
Felipe.



More information about the Haskell-Cafe mailing list