@@ -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)
20+import Data.IORef (IORef, newIORef, readIORef, writeIORef, atomicModifyIORef')
2121import Data.Streaming.Network (bindPortTCP)
2222import Foreign.C.Error (Errno(..), eCONNABORTED)
2323import GHC.IO.Exception (IOException(..))
@@ -59,11 +59,7 @@ import Network.Socket (fdSocket)
5959
6060-- | Creating 'Connection' for plain HTTP based on a given socket.
6161socketConnection :: Settings -> Socket -> IO Connection
62−#if MIN_VERSION_network(3,1,1)
6362socketConnection set s = do
64−#else
65−socketConnection _ s = do
66−#endif
6763 bufferPool <- newBufferPool
6864 writeBuf <- allocateBuffer bufferSize
6965 let sendall = Sock.sendAll s
@@ -80,7 +76,7 @@ socketConnection _ s = do
8076 if tm == 0 then
8177 close s
8278 else
83− gracefulClose s tm `E.catch` \(E.SomeException _) -> return ()
79+ gracefulClose s tm
8480#else
8581 , connClose = close s
8682#endif
@@ -286,7 +282,10 @@ fork :: Settings
286282fork set mkConn addr app counter ii = settingsFork set $ \unmask ->
287283 -- Call the user-supplied on exception code if any
288284 -- exceptions are thrown.
289− handle (settingsOnException set Nothing) $
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 ->
290289 -- Run the connection maker to get a new connection, and ensure
291290 -- that the connection is closed. If the mkConn call throws an
292291 -- exception, we will leak the connection. If the mkConn call is
@@ -297,13 +296,21 @@ fork set mkConn addr app counter ii = settingsFork set $ \unmask ->
297296 -- We grab the connection before registering timeouts since the
298297 -- timeouts will be useless during connection creation, due to the
299298 -- fact that async exceptions are still masked.
300− bracket mkConn cleanUp (serve unmask)
299+ bracket mkConn (cleanUp ref) (serve unmask ref)
301300 where
302− cleanUp (conn, _) = connClose conn `finally` connFree conn
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
303308
304309 -- We need to register a timeout handler for this thread, and
305− -- cancel that handler as soon as we exit.
306− serve unmask (conn, transport) = bracket register cancel $ \th -> do
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
307314 -- We now have fully registered a connection close handler in
308315 -- the case of all exceptions, so it is safe to once again
309316 -- allow async exceptions.
@@ -315,7 +322,8 @@ fork set mkConn addr app counter ii = settingsFork set $ \unmask ->
315322 -- above ensures the connection is closed.
316323 when goingon $ serveConnection conn ii th addr transport set app
317324 where
318− register = T.registerKillThread (timeoutManager ii) (connClose conn)
325+ register = T.registerKillThread (timeoutManager ii)
326+ (closeConn ref conn)
319327 cancel = T.cancel
320328
321329 onOpen adr = increase counter >> settingsOnOpen set adr
@@ -342,19 +350,11 @@ serveConnection conn ii th origAddr transport settings app = do
342350 istatus <- newIORef False
343351 if settingsHTTP2Enabled settings && h2 then do
344352 rawRecvN <- makeReceiveN bs (connRecv conn) (connRecvBuf conn)
345− -- This thread becomes the sender in http2 library.
346− -- In the case of event source, one request comes and one
347− -- worker gets busy. But it is likely that the receiver does
348− -- not receive any data at all while the sender is sending
349− -- output data from the worker. It's not good enough to tickle
350− -- the time handler in the receiver only. So, we should tickle
351− -- the time handler in both the receiver and the sender.
352353 let recvN = wrappedRecvN th istatus (settingsSlowlorisSize settings) rawRecvN
353− sendBS x = connSendAll conn x >> T.tickle th
354354 -- fixme: origAddr
355355 checkTLS
356356 setConnHTTP2 conn True
357− http2 conn transport ii origAddr settings recvN sendBS app
357+ http2 conn transport ii origAddr settings recvN app
358358 else do
359359 src <- mkSource (wrappedRecv conn th istatus (settingsSlowlorisSize settings))
360360 writeIORef istatus True