@@ -22,6 +22,7 @@ import GHC.IO.Exception (IOErrorType (..))
2222import System.IO.Error (ioeGetErrorType )
2323
2424import Control.Monad.Except (liftEither )
25+ import Control.Monad.Extra (whenJust )
2526import Data.Either.Combinators (mapLeft , whenLeft )
2627import Data.Maybe (fromJust )
2728import Data.String (IsString (.. ))
@@ -78,8 +79,10 @@ run appState = do
7879
7980 AppState. schemaCacheLoader appState -- Loads the initial SchemaCache
8081 (mainSocket, adminSocket) <- initSockets conf
81-
82- Unix. installSignalHandlers (AppState. getMainThreadId appState) (AppState. schemaCacheLoader appState) (AppState. readInDbConfig False appState)
82+ let closeSockets = do
83+ whenJust adminSocket NS. close
84+ NS. close mainSocket
85+ Unix. installSignalHandlers closeSockets (AppState. schemaCacheLoader appState) (AppState. readInDbConfig False appState)
8386
8487 Listener. runListener appState
8588
@@ -91,6 +94,10 @@ run appState = do
9194 address <- resolveSocketToAddress mainSocket
9295 observer $ AppServerAddressObs address
9396
97+ -- Hardcoding maximum graceful shutdown timeout (arbitrary set to 5 seconds)
98+ -- This is unfortunate but necessary becase graceful shutdowns don't work with HTTP keep-alive
99+ -- causing Warp to handle requests on already opened connections even if the listen socket is closed
100+ -- See: https://github.com/yesodweb/wai/issues/853
94101 Warp. runSettingsSocket (serverSettings conf & setOnException onWarpException) mainSocket app
95102 where
96103 observer = AppState. getObserver appState
0 commit comments