[Haskell-cafe] Re: Lazy IO and closing of file handles

Benjamin Franksen benjamin.franksen at bessy.de
Tue Mar 20 18:25:00 EDT 2007

Bertram Felgenhauer wrote:
> Having to rely on GC to close the fds quickly enough is another problem;
> can this be solved on the library side, maybe by performing GCs when
> running out of FDs?

Claus Reinke wrote:
> in good old Hugs, for instance, we find in function newHandle in
> [...snip...]
>     /* Search for unused handle*/
>     /* If at first we don't    */
>     /* succeed, garbage collect*/
>     /* and try again ...       */
>     /* ... before we give up   */
> so, instead of documenting limitations and workarounds, this issue should
> fixed in GHC as well.

This may help in some cases but it cannot be relied upon. Finalizers are
always run in a separate thread (must be, see
http://www.hpl.hp.com/techreports/2002/HPL-2002-335.html). Thus, even if
you force a GC when handles are exhausted, as hugs seems to do, there is no
guarantee that by the time the GC is done the finalizers have freed any
handles (assuming that the GC run really detects any handles to be


More information about the Haskell-Cafe mailing list