on master help sign in
The time manger does not close sockets to prevent "Bad file descriptor". (#739)

This ensures that ThreadKilled is caught on timeout.

commita0592ca2e6b9deed944ba275b3cbe7a7c0d5c8f9 authorKazu Yamamoto <kazu@iij.ad.jp> dateWed 8 Jan 2020, 23:25 (7 years ago) parentb0b04bf
1 file changed+7−19
warp/Network/Wai/Handler/Warp/Run.hs+7−19
warp/Network/Wai/Handler/Warp/Run.hs+7−19
@@ -17,7 +17,7 @@ import qualified Control.Concurrent as Conc (yield)
1717import Control.Exception as E
1818import qualified Data.ByteString as S
1919import Data.Char (chr)
20−import Data.IORef (IORef, newIORef, readIORef, writeIORef, atomicModifyIORef')
20+import Data.IORef (IORef, newIORef, readIORef, writeIORef)
2121import Data.Streaming.Network (bindPortTCP)
2222import Foreign.C.Error (Errno(..), eCONNABORTED)
2323import GHC.IO.Exception (IOException(..))
@@ -282,10 +282,7 @@ fork :: Settings
282282fork set mkConn addr app counter ii = settingsFork set $ \unmask ->
283283 -- Call the user-supplied on exception code if any
284284 -- exceptions are thrown.
285− handle (settingsOnException set Nothing) .
286− -- Allocate a new IORef indicating whether the connection has been
287− -- closed, to avoid double-freeing a connection
288− withClosedRef $ \ref ->
285+ handle (settingsOnException set Nothing) $
289286 -- Run the connection maker to get a new connection, and ensure
290287 -- that the connection is closed. If the mkConn call throws an
291288 -- exception, we will leak the connection. If the mkConn call is
@@ -296,21 +293,13 @@ fork set mkConn addr app counter ii = settingsFork set $ \unmask ->
296293 -- We grab the connection before registering timeouts since the
297294 -- timeouts will be useless during connection creation, due to the
298295 -- fact that async exceptions are still masked.
299− bracket mkConn (cleanUp ref) (serve unmask ref)
296+ bracket mkConn cleanUp (serve unmask)
300297 where
301− withClosedRef inner = newIORef False >>= inner
302−
303− closeConn ref conn = do
304− isClosed <- atomicModifyIORef' ref $ \x -> (True, x)
305− unless isClosed $ connClose conn
306−
307− cleanUp ref (conn, _) = closeConn ref conn `finally` connFree conn
298+ cleanUp (conn, _) = connClose conn `finally` connFree conn
308299
309300 -- We need to register a timeout handler for this thread, and
310− -- cancel that handler as soon as we exit. We additionally close
311− -- the connection immediately in case the child thread catches the
312− -- async exception or performs some long-running cleanup action.
313− serve unmask ref (conn, transport) = bracket register cancel $ \th -> do
301+ -- cancel that handler as soon as we exit.
302+ serve unmask (conn, transport) = bracket register cancel $ \th -> do
314303 -- We now have fully registered a connection close handler in
315304 -- the case of all exceptions, so it is safe to once again
316305 -- allow async exceptions.
@@ -322,8 +311,7 @@ fork set mkConn addr app counter ii = settingsFork set $ \unmask ->
322311 -- above ensures the connection is closed.
323312 when goingon $ serveConnection conn ii th addr transport set app
324313 where
325− register = T.registerKillThread (timeoutManager ii)
326− (closeConn ref conn)
314+ register = T.registerKillThread (timeoutManager ii) (return ())
327315 cancel = T.cancel
328316
329317 onOpen adr = increase counter >> settingsOnOpen set adr