refactor: make sure proper sockets cleanup is performed in App.run

Right now code in App.run does not properly use bracket/finally to close sockets and clean-up mainSocketRef. This is not a big problem at the moment because the application is going to exit enyway but introducting proper resource handling will make future refactorings safer.
This commit is contained in:
Michał Kłeczek
2026-07-06 10:59:05 -05:00
committed by Steve Chavez
parent 56df1dc532
commit c5c9dc33c9
+11 -7
View File
@@ -24,7 +24,6 @@ import GHC.IO.Exception (IOErrorType (..))
import System.IO.Error (ioeGetErrorType) import System.IO.Error (ioeGetErrorType)
import Control.Monad.Except (liftEither) import Control.Monad.Except (liftEither)
import Control.Monad.Extra (whenJust)
import Data.Either.Combinators (mapLeft, whenLeft) import Data.Either.Combinators (mapLeft, whenLeft)
import Data.IORef (atomicWriteIORef, newIORef, import Data.IORef (atomicWriteIORef, newIORef,
readIORef) readIORef)
@@ -83,11 +82,14 @@ run appState = do
conf <- AppState.getConfig appState conf <- AppState.getConfig appState
mainSocketRef <- newIORef Nothing mainSocketRef <- newIORef Nothing
adminSocket <- initAdminServerSocket conf let setMainSocketRef = atomicWriteIORef mainSocketRef . Just
clearMainSocketRef = atomicWriteIORef mainSocketRef Nothing
bracket (initAdminServerSocket conf) ensureSocketClosed $ \adminSocket -> do
let closeSockets = do let closeSockets = do
whenJust adminSocket NS.close ensureSocketClosed adminSocket
readIORef mainSocketRef >>= foldMap NS.close ensureSocketClosed =<< readIORef mainSocketRef
Unix.installSignalHandlers observer closeSockets (AppState.schemaCacheLoader appState) (AppState.readInDbConfig False appState) Unix.installSignalHandlers observer closeSockets (AppState.schemaCacheLoader appState) (AppState.readInDbConfig False appState)
Admin.runAdmin appState adminSocket (checkMainAppLive (readIORef mainSocketRef)) (serverSettings conf) Admin.runAdmin appState adminSocket (checkMainAppLive (readIORef mainSocketRef)) (serverSettings conf)
@@ -99,8 +101,7 @@ run appState = do
AppState.schemaCacheLoader appState AppState.schemaCacheLoader appState
AppState.waitForSchemaCacheInit appState AppState.waitForSchemaCacheInit appState
mainSocket <- initServerSocket conf bracket (initServerSocket conf) NS.close $ \mainSocket -> do
atomicWriteIORef mainSocketRef $ Just mainSocket
let app = postgrest appState (AppState.schemaCacheLoader appState) let app = postgrest appState (AppState.schemaCacheLoader appState)
@@ -110,12 +111,15 @@ run appState = do
appServerSettings = serverSettings conf appServerSettings = serverSettings conf
& setPort (configServerPort conf) & setPort (configServerPort conf)
& setOnException onWarpException & setOnException onWarpException
& setBeforeMainLoop (observer $ AppServerAddressObs address) & setBeforeMainLoop (setMainSocketRef mainSocket *> observer (AppServerAddressObs address))
Warp.runSettingsSocket appServerSettings mainSocket app Warp.runSettingsSocket appServerSettings mainSocket app
`finally` clearMainSocketRef
where where
observer = AppState.getObserver appState observer = AppState.getObserver appState
ensureSocketClosed = foldMap NS.close
onWarpException :: Maybe Wai.Request -> SomeException -> IO () onWarpException :: Maybe Wai.Request -> SomeException -> IO ()
onWarpException _ ex = onWarpException _ ex =
when (shouldDisplayException ex) $ when (shouldDisplayException ex) $