[Haskell-cafe] On hGetContents semi-closenesscloseness

Chris Smith cdsmith at gmail.com
Tue Feb 15 20:15:26 CET 2011


On Tue, 2011-02-15 at 14:06 -0500, Brandon S Allbery KF8NH wrote:
> > entirely consumed that the descriptor gets closed. Is hGetContents responsible
> > for closing the descriptor? Or is it the garbage collector? Who closes the
> > descriptor when the contents are read? Looking at hGetContents function
> 
> The garbage collector closes the handle, as I understand it.

The handle is actually closed as soon as you read all the way to the end
of the file.  However, because reading is done as a side effect of
forcing a lazy, supposedly-pure value, it's hard to predict when that
will happen, and apprently unrelated changes in a different part of the
program can cause it to never happen and cause you to leak file handles.

In a way, it's analogous to the situation with garbage collection and
closing file handles in finalizers; but the details are different and
the unpredictable file closing comes from lazy evaluation rather than
garbage collection.

-- 
Chris Smith




More information about the Haskell-Cafe mailing list