Skip to content

Commit c77c429

Browse files
committed
fix: shutdown should wait for in flight requests
Upgraded warp to 3.4.13 which fixed yesodweb/wai#853 Changed interrupt handling so that instead of killing the main thread, listening sockets are closed which triggers warp graceful shutdown.
1 parent 6b4022b commit c77c429

8 files changed

Lines changed: 66 additions & 7 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ All notable changes to this project will be documented in this file. From versio
1515
- Add config `db-timezone-enabled` for optional querying of timezones by @taimoorzaeem in #4751
1616
- Log schema cache queries timings on `log-level=debug` by @steve-chavez in #4805
1717

18+
### Fixed
19+
20+
- Shutdown should wait for in flight requests by @mkleczek in #4702
21+
1822
### Changed
1923

2024
- Drop support for PostgreSQL EOL version 13 by @wolfgangwalther in #4193

nix/overlays/haskell-packages.nix

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,43 @@ let
5959
postgresql-binary = lib.dontCheck (lib.doJailbreak prev.postgresql-binary_0_13_1_3);
6060
text-builder = prev.text-builder_0_6_10;
6161
text-builder-dev = prev.text-builder-dev_0_3_10;
62+
63+
http2 =
64+
prev.callHackageDirect
65+
{
66+
pkg = "http2";
67+
ver = "5.4.0";
68+
sha256 = "sha256-PeEWVd61bQ8G7LvfLeXklzXqNJFaAjE2ecRMWJZESPE=";
69+
}
70+
{ };
71+
72+
http-semantics =
73+
prev.callHackageDirect
74+
{
75+
pkg = "http-semantics";
76+
ver = "0.4.0";
77+
sha256 = "sha256-rh0z51EKvsu5rQd5n2z3fSRjjEObouNZSBPO9NFYOF0=";
78+
}
79+
{ };
80+
81+
network-run =
82+
prev.callHackageDirect
83+
{
84+
pkg = "network-run";
85+
ver = "0.5.0";
86+
sha256 = "sha256-vbXh+CzxDsGApjqHxCYf/ijpZtUCApFbkcF5gyN0THU=";
87+
}
88+
{ };
89+
90+
warp =
91+
lib.dontCheck (prev.callHackageDirect
92+
{
93+
pkg = "warp";
94+
ver = "3.4.13";
95+
sha256 = "sha256-jmr8kpeSPDkOhT0i9PhozZapX4nUs92cOX7POAGb7/M=";
96+
}
97+
{ });
98+
6299
};
63100
in
64101
{

postgrest.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ library
156156
-- for unix sockets; this is tested in test/io/test_io.py. See
157157
-- https://github.com/kazu-yamamoto/logger/commit/3a71ca70afdbb93d4ecf0083eeba1fbbbcab3fc3
158158
, wai-logger >= 2.4.0
159-
, warp >= 3.3.19 && < 3.5
159+
, warp >= 3.4.13 && < 3.5
160160
, stm >= 2.5 && < 3
161161
, stm-hamt >= 1.2 && < 2
162162
, focus >= 1.0 && < 2

src/PostgREST/App.hs

Lines changed: 5 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

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

stack.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@ extra-deps:
1616
- hasql-notifications-0.2.2.2
1717
- hasql-pool-1.0.1
1818
- hasql-transaction-1.1.0.1
19+
- http-semantics-0.4.0
20+
- http2-5.4.0
1921
- postgresql-binary-0.13.1.3
2022
- text-builder-0.6.10
2123
- text-builder-dev-0.3.10
24+
- warp-3.4.13
2225

2326
allow-newer: true
2427
allow-newer-deps:

stack.yaml.lock

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,20 @@ packages:
6060
size: 1027
6161
original:
6262
hackage: hasql-transaction-1.1.0.1
63+
- completed:
64+
hackage: http-semantics-0.4.0@sha256:da8a98d542b2032cc12590847179577b0208a52bb3b9aa9a07c08d27d2a1714c,1513
65+
pantry-tree:
66+
sha256: d0e08875907c0fbff71813747fd93ce7bfd4e3d4a6b979df5c137430a9130f1d
67+
size: 1188
68+
original:
69+
hackage: http-semantics-0.4.0
70+
- completed:
71+
hackage: http2-5.4.0@sha256:1e9f6f5f32bfb3176136f35e041aa279bc456e81d4674ddaaeaa7c0d091be0c7,10624
72+
pantry-tree:
73+
sha256: 5c89815392d85d854efe75cf2279f58ff9f5e4b8fc1f83c55de93191edd128da
74+
size: 44864
75+
original:
76+
hackage: http2-5.4.0
6377
- completed:
6478
hackage: postgresql-binary-0.13.1.3@sha256:4de5ddc90d9d3e586c3edf2860280a0915a484e9b8de3f36316a4cab2b330852,4037
6579
pantry-tree:

test/io/test_io.py

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

130130

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

0 commit comments

Comments
 (0)