@@ -263,45 +263,24 @@ fork :: Settings
263263 -> Counter
264264 -> InternalInfo0
265265 -> IO ()
266−fork set mkConn addr app counter ii0 = settingsFork set $ \ unmask ->
266+fork set mkConn addr app counter ii0 = settingsFork set $ \unmask ->
267267 -- Call the user-supplied on exception code if any
268268 -- exceptions are thrown.
269269 handle (settingsOnException set Nothing) .
270−
271270 -- Allocate a new IORef indicating whether the connection has been
272271 -- closed, to avoid double-freeing a connection
273272 withClosedRef $ \ref ->
274− let cleanUp (conn, _) = closeConn ref conn `finally` connFree conn
275− serve (conn, transport) =
276− -- We need to register a timeout handler for this thread, and
277− -- cancel that handler as soon as we exit. We additionally close
278− -- the connection immediately in case the child thread catches the
279− -- async exception or performs some long-running cleanup action.
280− bracket (T.registerKillThread (timeoutManager0 ii0) (closeConn ref conn))
281− T.cancel $ \th ->
282− let ii1 = toInternalInfo1 ii0 th
283− -- We now have fully registered a connection close handler
284− -- in the case of all exceptions, so it is safe to one
285− -- again allow async exceptions.
286− in unmask .
287−
288− -- Call the user-supplied code for connection open and close events
289− bracket (onOpen addr) (onClose addr) $ \goingon ->
290−
291− -- Actually serve this connection.
292− -- bracket with closeConn above ensures the connection is closed.
293− when goingon $ serveConnection conn ii1 addr transport set app
294− -- Run the connection maker to get a new connection, and ensure
295− -- that the connection is closed. If the mkConn call throws an
296− -- exception, we will leak the connection. If the mkConn call is
297− -- vulnerable to attacks (e.g., Slowloris), we do nothing to
298− -- protect the server. It is therefore vital that mkConn is well
299− -- vetted.
300− --
301− -- We grab the connection before registering timeouts since the
302− -- timeouts will be useless during connection creation, due to the
303− -- fact that async exceptions are still masked.
304− in bracket mkConn cleanUp serve
273+ -- Run the connection maker to get a new connection, and ensure
274+ -- that the connection is closed. If the mkConn call throws an
275+ -- exception, we will leak the connection. If the mkConn call is
276+ -- vulnerable to attacks (e.g., Slowloris), we do nothing to
277+ -- protect the server. It is therefore vital that mkConn is well
278+ -- vetted.
279+ --
280+ -- We grab the connection before registering timeouts since the
281+ -- timeouts will be useless during connection creation, due to the
282+ -- fact that async exceptions are still masked.
283+ bracket mkConn (cleanUp ref) (serve unmask ref)
305284 where
306285 withClosedRef inner = newIORef False >>= inner
307286
@@ -309,6 +288,29 @@ fork set mkConn addr app counter ii0 = settingsFork set $ \ unmask ->
309288 isClosed <- atomicModifyIORef' ref $ \x -> (True, x)
310289 unless isClosed $ connClose conn
311290
291+ cleanUp ref (conn, _) = closeConn ref conn `finally` connFree conn
292+
293+ -- We need to register a timeout handler for this thread, and
294+ -- cancel that handler as soon as we exit. We additionally close
295+ -- the connection immediately in case the child thread catches the
296+ -- async exception or performs some long-running cleanup action.
297+ serve unmask ref (conn, transport) = bracket register cancel $ \th -> do
298+ let ii1 = toInternalInfo1 ii0 th
299+ -- We now have fully registered a connection close handler in
300+ -- the case of all exceptions, so it is safe to one again
301+ -- allow async exceptions.
302+ unmask .
303+ -- Call the user-supplied code for connection open and
304+ -- close events
305+ bracket (onOpen addr) (onClose addr) $ \goingon ->
306+ -- Actually serve this connection. bracket with closeConn
307+ -- above ensures the connection is closed.
308+ when goingon $ serveConnection conn ii1 addr transport set app
309+ where
310+ register = T.registerKillThread (timeoutManager0 ii0)
311+ (closeConn ref conn)
312+ cancel = T.cancel
313+
312314 onOpen adr = increase counter >> settingsOnOpen set adr
313315 onClose adr _ = decrease counter >> settingsOnClose set adr
314316