[Haskell-cafe] interrupting an accept()ing thread

Lukas Mai l.mai at web.de
Fri Jul 6 03:57:14 EDT 2007

Am Freitag, 6. Juli 2007 04:15 schrieb Thomas Conway:
> On 7/6/07, Lukas Mai <l.mai at web.de> wrote:
> > I don't see how this solves the problem. AFAICS acceptLoop never returns
> > and sok is never closed. On the other hand, my program doesn't need a
> > liveOpCount because the subthreads take care of themselves. It's just the
> > accept loop I need to break somehow.
> Well, it works because the sub-thread dies when the program exits, so
> the socket gets closed then.

But I don't want to exit the program, I just want to close the socket.
(There may be more than one such loop running, all independent from
each other.) My current solution looks like this:

killSocket :: Socket -> IO ()
killSocket s = do
    nul <- openFd "/dev/null" WriteOnly Nothing defaultFileFlags
    dupTo nul (Fd $ fdSocket s)
    closeFd nul

This makes accept blow up because s no longer refers to a socket. The
resulting exception is caught and the handler closes s, avoiding a file
descriptor leak.

While this works fine, it seems like a kludge. I'm open to suggestions
on how to improve this. :-)


More information about the Haskell-Cafe mailing list