Skip to content

Commit ec1782b

Browse files
committed
fix: shutdown should wait for in flight requests
1 parent e70c925 commit ec1782b

4 files changed

Lines changed: 14 additions & 6 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ All notable changes to this project will be documented in this file. From versio
2121
- Log error when `db-schemas` config contains schema `pg_catalog` or `information_schema` by @taimoorzaeem in #4359
2222
+ Now fails at startup. Prior to this, it failed with `PGRST205` on requests related to these schemas.
2323

24+
### Fixed
25+
- Shutdown should wait for in flight requests by @mkleczek in #4702
26+
2427
## [14.7] - 2026-03-20
2528

2629
### Fixed

src/PostgREST/App.hs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import GHC.IO.Exception (IOErrorType (..))
2222
import System.IO.Error (ioeGetErrorType)
2323

2424
import Control.Monad.Except (liftEither)
25+
import Control.Monad.Extra (whenJust)
2526
import Data.Either.Combinators (mapLeft, whenLeft)
2627
import Data.Maybe (fromJust)
2728
import Data.String (IsString (..))
@@ -79,8 +80,10 @@ run appState = do
7980

8081
AppState.schemaCacheLoader appState -- Loads the initial SchemaCache
8182
(mainSocket, adminSocket) <- initSockets conf
82-
83-
Unix.installSignalHandlers observer (AppState.getMainThreadId appState) (AppState.schemaCacheLoader appState) (AppState.readInDbConfig False appState)
83+
let closeSockets = do
84+
whenJust adminSocket NS.close
85+
NS.close mainSocket
86+
Unix.installSignalHandlers observer closeSockets (AppState.schemaCacheLoader appState) (AppState.readInDbConfig False appState)
8487

8588
Listener.runListener appState
8689

@@ -92,6 +95,10 @@ run appState = do
9295
address <- resolveSocketToAddress mainSocket
9396
observer $ AppServerAddressObs address
9497

98+
-- Hardcoding maximum graceful shutdown timeout (arbitrary set to 5 seconds)
99+
-- This is unfortunate but necessary becase graceful shutdowns don't work with HTTP keep-alive
100+
-- causing Warp to handle requests on already opened connections even if the listen socket is closed
101+
-- See: https://github.com/yesodweb/wai/issues/853
95102
Warp.runSettingsSocket (serverSettings conf & setOnException onWarpException) mainSocket app
96103
where
97104
observer = AppState.getObserver appState

src/PostgREST/Unix.hs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,9 @@ import System.Directory (removeFile)
1919
import System.IO.Error (isDoesNotExistError)
2020

2121
-- | Set signal handlers, only for systems with signals
22-
installSignalHandlers :: Observation.ObservationHandler -> ThreadId -> IO () -> IO () -> IO ()
22+
installSignalHandlers :: Observation.ObservationHandler -> IO () -> IO () -> IO () -> IO ()
2323
#ifndef mingw32_HOST_OS
24-
installSignalHandlers observer tid usr1 usr2 = do
25-
let interrupt = throwTo tid UserInterrupt
24+
installSignalHandlers observer interrupt usr1 usr2 = do
2625
install Signals.sigINT $ observer (Observation.TerminationUnixSignalObs "SIGINT") >> interrupt
2726
install Signals.sigTERM $ observer (Observation.TerminationUnixSignalObs "SIGTERM") >> interrupt
2827
install Signals.sigUSR1 usr1

test/io/test_io.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ def sleep():
105105
t.join()
106106

107107

108-
@pytest.mark.xfail(reason="Graceful shutdown is currently failing", strict=True)
109108
def test_graceful_shutdown_waits_for_in_flight_request(defaultenv):
110109
"SIGTERM should allow in-flight requests to finish before exiting"
111110

0 commit comments

Comments
 (0)