@@ -35,6 +35,7 @@ import qualified Network.Wai.Handler.Warp.FdCache as F
35import qualified Network.Wai.Handler.Warp.FileInfoCache as I
35import qualified Network.Wai.Handler.Warp.FileInfoCache as I
36import Network.Wai.Handler.Warp.HTTP2 (http2, isHTTP2)
36import Network.Wai.Handler.Warp.HTTP2 (http2, isHTTP2)
37import Network.Wai.Handler.Warp.Header
37import Network.Wai.Handler.Warp.Header
38+import Network.Wai.Handler.Warp.IORef
38import Network.Wai.Handler.Warp.ReadInt
39import Network.Wai.Handler.Warp.ReadInt
39import Network.Wai.Handler.Warp.Recv
40import Network.Wai.Handler.Warp.Recv
40import Network.Wai.Handler.Warp.Request
41import Network.Wai.Handler.Warp.Request
@@ -262,6 +263,10 @@ fork :: Settings
262 -> InternalInfo0
263 -> InternalInfo0
264fork set mkConn addr app counter ii0 = settingsFork set $ \ unmask ->
265fork set mkConn addr app counter ii0 = settingsFork set $ \ unmask ->
266+ -- Allocate a new IORef indicating whether the connection has been
267+ -- closed, to avoid double-freeing a connection
268+ withClosedRef $ \ref ->
265 -- Run the connection maker to get a new connection, and ensure
270 -- Run the connection maker to get a new connection, and ensure
266 -- that the connection is closed. If the mkConn call throws an
271 -- that the connection is closed. If the mkConn call throws an
267 -- exception, we will leak the connection. If the mkConn call is
272 -- exception, we will leak the connection. If the mkConn call is
@@ -272,11 +277,13 @@ fork set mkConn addr app counter ii0 = settingsFork set $ \ unmask ->
272 -- We grab the connection before registering timeouts since the
277 -- We grab the connection before registering timeouts since the
273 -- timeouts will be useless during connection creation, due to the
278 -- timeouts will be useless during connection creation, due to the
274 -- fact that async exceptions are still masked.
279 -- fact that async exceptions are still masked.
275− bracket mkConn closeConn $ \(conn, transport) ->
280+ bracket mkConn (closeConn ref . fst) $ \(conn, transport) ->
277 -- We need to register a timeout handler for this thread, and
282 -- We need to register a timeout handler for this thread, and
278− -- cancel that handler as soon as we exit.
283+ -- cancel that handler as soon as we exit. We additionally close
284+ -- the connection immediately in case the child thread catches the
285+ -- async exception or performs some long-running cleanup action.
279− bracket (T.registerKillThread (timeoutManager0 ii0)) T.cancel $ \th ->
286+ bracket (T.registerKillThread (timeoutManager0 ii0) (closeConn ref conn)) T.cancel $ \th ->
281 let ii1 = toInternalInfo1 ii0 th
288 let ii1 = toInternalInfo1 ii0 th
282 -- We now have fully registered a connection close handler
289 -- We now have fully registered a connection close handler
@@ -294,7 +301,11 @@ fork set mkConn addr app counter ii0 = settingsFork set $ \ unmask ->
294 -- bracket with closeConn above ensures the connection is closed.
301 -- bracket with closeConn above ensures the connection is closed.
295 when goingon $ serveConnection conn ii1 addr transport set app
302 when goingon $ serveConnection conn ii1 addr transport set app
304+ withClosedRef inner = newIORef False >>= inner
306+ closeConn ref conn = do
307+ isClosed <- atomicModifyIORef' ref $ \x -> (True, x)
297− closeConn (conn, _transport) = connClose conn
308+ unless isClosed $ connClose conn
299 onOpen adr = increase counter >> settingsOnOpen set adr
310 onOpen adr = increase counter >> settingsOnOpen set adr
300 onClose adr _ = decrease counter >> settingsOnClose set adr
311 onClose adr _ = decrease counter >> settingsOnClose set adr