[Haskell-cafe] addFinalizer in GHC 7.10

Kazu Yamamoto (=?iso-2022-jp?B?GyRCOzNLXE9CSScbKEI=?=) kazu at iij.ad.jp
Tue Jan 30 03:25:43 UTC 2018


Hello cafe,

We, the network library maintainers, have questions about
System.Mem.Weak.addFinalizer in GHC 7.10.

We are trying to make the library extensible especially for the socket
type and the socket address type. For this purpose, a proposed
definition for Socket in version 3.0.0.0 is a just CInt:

    newtype Socket = Socket CInt deriving (Eq, Show)

One problem is that unreachable Socket cannot be GCed if CInt (a file
descriptor) is not closed. To let GC work, we are trying to add a
finalizer via addFinalizer:

    socket family stype protocol = do
       fd <- c_socket ...
       ...
       let s = Socket fd
       addFinalizer s $ close s
       ruturn s

This works well for many cases. Unfortunately, we *sometime* got the
following error with GHC 7.10:

    tests/SimpleSpec.hs:56: 
      1) Simple.sendMany works well
           uncaught exception: IOException of type InvalidArgument (threadWait: invalid argument (Bad file descriptor))

It seems to us that Socket is GCed too early in GHC 7.10. We don't
see this behavior in other versions of GHC.

So, here are our questions:

Q1) Do we use addFinalizer correctly?
Q2) Is this a bug of GHC 7.10?
Q3) If this is a bug of GHC 7.10, are there any workarounds?

--Kazu


More information about the Haskell-Cafe mailing list