From 67a31949037990513021d30659fac72a76e226a9 Mon Sep 17 00:00:00 2001 From: Joe Nelson Date: Mon, 7 Dec 2015 16:40:12 -0800 Subject: [PATCH] Release all db connections on ctrl-c Fixes #268 --- postgrest.cabal | 1 + src/PostgREST/Main.hs | 12 +++++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/postgrest.cabal b/postgrest.cabal index 6ad23dfa6..54ab5b78c 100644 --- a/postgrest.cabal +++ b/postgrest.cabal @@ -61,6 +61,7 @@ executable postgrest , HTTP, http-types , MissingH , Ranged-sets + , unix >= 2.7 && < 3 hs-source-dirs: src other-modules: Paths_postgrest diff --git a/src/PostgREST/Main.hs b/src/PostgREST/Main.hs index 8faec1be8..a331f197f 100644 --- a/src/PostgREST/Main.hs +++ b/src/PostgREST/Main.hs @@ -10,7 +10,9 @@ import PostgREST.Error (pgErrResponse, PgError) import PostgREST.Middleware import PostgREST.DbStructure -import Control.Monad (unless) +import Control.Concurrent (myThreadId) +import Control.Exception.Base (throwTo, AsyncException(..)) +import Control.Monad (unless, void) import Control.Monad.IO.Class (liftIO) import Data.Aeson (encode) import Data.Functor.Identity @@ -26,6 +28,7 @@ import Network.Wai.Middleware.RequestLogger (logStdout) import System.IO (BufferMode (..), hSetBuffering, stderr, stdin, stdout) +import System.Posix.Signals import Web.JWT (secret) isServerVersionSupported :: H.Session P.Postgres IO Bool @@ -69,6 +72,13 @@ main = do <> show minimumPgVersion) ) supportedOrError + tid <- myThreadId + void $ installHandler keyboardSignal (Catch $ do + putStrLn "\nReleasing connection pool..." + H.releasePool pool + throwTo tid UserInterrupt + ) Nothing + let txSettings = Just (H.ReadCommitted, Just True) dbOrError <- H.session pool $ H.tx txSettings $ getDbStructure (cs $ configSchema conf) dbStructure <- either hasqlError return dbOrError