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

Donn Cave donn at avvanta.com
Sun Jul 17 18:40:31 CEST 2011


Quoth Felipe Almeida Lessa <felipe.lessa at gmail.com>,
> 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.

The use of withFile on the main function is a good practice in Haskell
only because of this defect in the GHC library implementation.

There's no question, if there were two competing Haskell library
implementations, GHC and one that worked like buffered I/O in other
languages, which one would better support Haskell programmers.
It's too bad that doesn't qualify it as "valid" bug.

	Donn



More information about the Haskell-Cafe mailing list