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

Rich Neswold rich.neswold at gmail.com
Sat Jul 7 00:38:47 EDT 2007


On 7/5/07, Lukas Mai <l.mai at web.de> wrote:
>
> Hello, cafe!
>
> I have the following code (paraphrased):
>
> ...
> forkIO spin
> ...
> spin = do
>     (t, _) <- accept s   -- (*)
>     forkIO $ dealWith t  -- (**)
>     spin
>
> My problem is that I want to stop spin from another thread. The "obvious"
> solution would be to throw it an exception. However, that leaks a socket
> (t) if the exception arrives between (*) and (**). I could wrap the whole
> thing in block, but from looking at the source of Network.Socket it seems
> that accept itself is not exception safe; so no matter what I do, I can't
> use asynchronous exceptions to make spin exit.
>

What about using "bracketOnError"?

nextClient s = bracketOnError (fst . accept s) sClose

spin = do
    nextClient s (\s' -> forkIO $ dealWith s')
    spin

If "bracketOnError" leaks the resource in the event of an exception, then it
needs to be fixed.

-- 
Rich

JID: rich at neswold.homeunix.net
AIM: rnezzy
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20070706/2fa4972b/attachment-0001.htm


More information about the Haskell-Cafe mailing list