[Haskell-cafe] Re: HXT 7.4/7.5 leaking TCP connections

Uwe Schmidt uwe at fh-wedel.de
Tue Apr 8 07:40:30 EDT 2008


Daniel McAllansmith wrote:

> I'm having some problems with connections leaking when using 7.4.  I've
> just checked and it happens with 7.5 off of hackage as well.
>
> It appears that readDocument doesn't close the connection when using the
> haskell http library, it works ok when curl is used though.
>
> Following is a program which demonstrates the problem.  /tmp/list.txt just
> has the name of files available from my local webserver, more than 1024
> files is enough to exhaust my open file limit.
>
> I think the garbage collector does collect the connections, but they don't
> end up fully closed... that's another issue I'm planning on looking at.
>
> If you need any more information please ask.  I may get a chance to try and
> write a patch but using curl is a work around while I'm busy.
> ....

this is a known problem with HTTP package (version 3001.0.4).
Paul Brown has described this somewere in his blog.
(http://mult.ifario.us/t/haskell), but my firefox only shows
an incomplete page of this blog, the solution is missing.
Paul promissed in his blog to send a patch to Björn Bringert.

Here is the solution:

edit the close function in HTTP/TCP.hs as follows:

----------------------------------------------------------------
 
close ref = 
    do { c <- readIORef (getRef ref)
       ; closeConn c
       ; writeIORef (getRef ref) ConnClosed
       }
    where
        -- Be kind to peer & close gracefully.
        closeConn (ConnClosed) = return ()
        closeConn (MkConn sk addr [] _) =
            mapM_ (flip Exception.catch $ \_ -> return ())
                  [ shutdown sk ShutdownSend
                  , suck ref
                  , shutdown sk ShutdownReceive
                  , sClose sk ]

        suck :: Connection -> IO ()
        suck cn = readLine cn >>= 
                  either (\_ -> return ()) -- catch errors & ignore
                         (\x -> if null x then return () else suck cn)

---------------------------------------------------------------------

I hope this will help.

Cheers

Uwe

P.S. to Björn: It would be nice, if you could include this change into the 
HTTP module and upload a new version to hackage.



More information about the Haskell-Cafe mailing list