[Haskell-cafe] network's example echo server seems leaking socket on error binding

Compl Yue compl.yue at gmail.com
Sat May 9 08:15:41 UTC 2020

Hi folks,

I just filed an issue https://github.com/haskell/network/issues/447 <https://github.com/haskell/network/issues/447> with the network package, I'd like to post here too in seeking your wisedom at the same time.

In top-level doc, the minimal example echo server uses Control.Exception.bracket like this:

    E.bracket (open addr) close loop
    resolve = do
        let hints = defaultHints {
                addrFlags = [AI_PASSIVE]
              , addrSocketType = Stream
        head <$> getAddrInfo (Just hints) mhost (Just port)
    open addr = do
        sock <- socket (addrFamily addr) (addrSocketType addr) (addrProtocol addr)
        setSocketOption sock ReuseAddr 1
        withFdSocket sock setCloseOnExecIfNeeded
        bind sock $ addrAddress addr
        listen sock 1024
        return sock
    loop sock = forever $ do
        (conn, _peer) <- accept sock
        void $ forkFinally (server conn) (const $ gracefulClose conn 5000)
I happened to copy the configuration with another machine's IP address to run on my dev machine, then of course it failed binding to the IP, but in this case I suspect sock above is leaked without close, as open failed at all, so sock is not given to bracket for it to do the cleanup.

Is my suspicion correct ? Or I missed something that the example actually won't leak on binding errors ?

Best regards,

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20200509/acd76587/attachment.html>

More information about the Haskell-Cafe mailing list