[Haskell-cafe] Fwd: Safe TCP accept loop

Sumit Raja sumitraja at gmail.com
Wed Oct 25 05:52:33 UTC 2017


Hi Ă–mer

> You need to mask async exceptions between `accept()` and cleanup action
> registration, because an exception in between these operations will cause the
> socket to leak.
>
> You can take a look at warp's accept loop:
>
> https://github.com/yesodweb/wai/blob/master/warp/Network/Wai/Handler/Warp/Run.hs#L211
>

Trying to map steps for the code you've pointed me to in bad pseudo code:

finally (mask >> acceptLoop serverSocket) (close serverSocket)

acceptLoop =
  unmask
  sock <- accept serverSock
  mask
    forkIO $ do
       mask
       finally (unmask >> process sock) (close sock)
  acceptLoop

Is this correct?

Thanks
Sumit


More information about the Haskell-Cafe mailing list