[Haskell-cafe] addFinalizer in GHC 7.10

Viktor Dukhovni ietf-dane at dukhovni.org
Tue Jan 30 04:50:06 UTC 2018



> On Jan 29, 2018, at 10:25 PM, Kazu Yamamoto (山本和彦) <kazu at iij.ad.jp> wrote:
> 
>    socket family stype protocol = do
>       fd <- c_socket ...
>       ...
>       let s = Socket fd
>       addFinalizer s $ close s
>       ruturn s

For the record, I think I've convinced Kazu Yamamoto that this is
an anti-pattern.  Such a finalizer would easily end up closing
already closed sockets, whose file-descriptors may already be
associated with other open files or sockets.  That way lie all
sorts of difficult to isolate race-condition bugs.  To make this
safe, the close function would need to mutate the socket,
invalidating the enclosed file-descriptor, and would then need to
be a NOP or just raise an exception if the socket is closed again
(the finalizer should invoke a close variant that just returns
without raising exceptions if the socket is already closed).

There is, AFAIK still an unresolved bug along these lines somewhere
in http-client and its dependencies.  So far no reproducing cases have
been provided.  No very recent reports either, perhaps it went away,
or people have just been more lucky lately:

   https://github.com/snoyberg/http-client/issues/252
   https://github.com/vincenthz/hs-tls/issues/179

All that said, the original question about addFinalizer vs. GHC 7.10
may still be worth exploring, even if its use-case for Sockets goes
away.  So, please don't take this poset to mean that the original
question should be ignored.

-- 
	Viktor.



More information about the Haskell-Cafe mailing list