From b85f2d54edf74efb8fb58986f7593ffab8030054 Mon Sep 17 00:00:00 2001 From: "Evgeny @ SimpleX Chat" <259188159+evgeny-simplex@users.noreply.github.com> Date: Mon, 9 Nov 2026 19:59:15 +0000 Subject: [PATCH] ntf server: prevent hanging on server stop (#2041) * ntf server: prevent hanging on server stop * lib: fail database operations on a closed PostgreSQL store --- .../Messaging/Agent/Store/Postgres/Common.hs | 11 ++++++----- tests/CoreTests/MsgStoreTests.hs | 16 +++++++++++++++- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/Simplex/Messaging/Agent/Store/Postgres/Common.hs b/src/Simplex/Messaging/Agent/Store/Postgres/Common.hs index b38b7ecbd..267e4e8a9 100644 --- a/src/Simplex/Messaging/Agent/Store/Postgres/Common.hs +++ b/src/Simplex/Messaging/Agent/Store/Postgres/Common.hs @@ -29,6 +29,7 @@ import Data.Functor (($>)) import qualified Database.PostgreSQL.Simple as PSQL import Numeric.Natural (Natural) import Simplex.Messaging.Agent.Store.Postgres.Options +import Simplex.Messaging.Util (ifM) -- TODO [postgres] use log_min_duration_statement instead of custom slow queries (SQLite's Connection type) data DBStore = DBStore @@ -56,14 +57,14 @@ data DBStorePool = DBStorePool } withConnectionPriority :: DBStore -> Bool -> (PSQL.Connection -> IO a) -> IO a -withConnectionPriority DBStore {dbPriorityPool, dbPool, dbConnect} priority = - withConnectionPool (if priority then dbPriorityPool else dbPool) dbConnect +withConnectionPriority DBStore {dbPriorityPool, dbPool, dbConnect, dbClosed} priority = + withConnectionPool (if priority then dbPriorityPool else dbPool) dbClosed dbConnect {-# INLINE withConnectionPriority #-} -withConnectionPool :: DBStorePool -> IO PSQL.Connection -> (PSQL.Connection -> IO a) -> IO a -withConnectionPool DBStorePool {dbPoolConns, dbSem} dbConnect action = +withConnectionPool :: DBStorePool -> TVar Bool -> IO PSQL.Connection -> (PSQL.Connection -> IO a) -> IO a +withConnectionPool DBStorePool {dbPoolConns, dbSem} dbClosed dbConnect action = E.mask $ \restore -> do - conn <- withMVar dbSem $ \_ -> atomically $ readTBQueue dbPoolConns + conn <- withMVar dbSem $ \_ -> atomically $ ifM (readTVar dbClosed) (throwSTM $ userError "withConnection: database closed") (readTBQueue dbPoolConns) r <- restore (action conn) `E.onException` reset conn atomically $ writeTBQueue dbPoolConns conn pure r diff --git a/tests/CoreTests/MsgStoreTests.hs b/tests/CoreTests/MsgStoreTests.hs index badf7f2f1..92f7b3abf 100644 --- a/tests/CoreTests/MsgStoreTests.hs +++ b/tests/CoreTests/MsgStoreTests.hs @@ -42,7 +42,8 @@ import Util #if defined(dbServerPostgres) import AgentTests.FunctionalAPITests (runRight) -import Control.Concurrent (threadDelay) +import Control.Concurrent (forkIO, threadDelay) +import qualified Control.Exception as E import Database.PostgreSQL.Simple (Only (..)) import qualified Database.PostgreSQL.Simple as DB import Simplex.Messaging.Agent.Store.Postgres.Common @@ -50,6 +51,7 @@ import Simplex.Messaging.Agent.Store.Shared (MigrationConfirmation (..)) import Simplex.Messaging.Server.MsgStore.Postgres import Simplex.Messaging.Server.QueueStore.Postgres import SMPClient (testStoreDBOpts) +import System.Timeout (timeout) #endif msgStoreTests :: Spec @@ -63,6 +65,7 @@ msgStoreTests = do it "should correctly update message counts and canWrite flag" $ withMsgStore testPostgresStoreConfig testUpdateMessageCounts it "tryDelPeekMsg (ACK not from NSE) should reset message counts when queue is empty" $ withMsgStore testPostgresStoreConfig testResetMessageCounts it "should expire messages across commit batches" $ withMsgStore testPostgresStoreConfig testExpireMessagesInBatches + it "should fail database operations after the store is closed" $ withMsgStore testPostgresStoreConfig testClosedStore #endif describe "Notification store" $ do it "should remove keys of expired notifications" testExpireNtfs @@ -443,4 +446,15 @@ testExpireMessagesInBatches ms = do fillPastQuota q = go 0 where go n = write q "fill" >>= maybe (pure n) (const $ go (n + 1)) + +testClosedStore :: PostgresMsgStore -> IO () +testClosedStore ms = do + closeMsgStore ms + r <- newEmptyTMVarIO + _ <- forkIO $ E.uninterruptibleMask_ (E.try $ withTransaction (dbStore $ queueStore ms) (`DB.query_` "SELECT 1")) >>= atomically . putTMVar r + res <- timeout 5000000 $ atomically $ readTMVar r + case res of + Just (Left (_ :: E.SomeException)) -> pure () + Just (Right (_ :: [Only Int])) -> expectationFailure "query succeeded on a closed store" + Nothing -> expectationFailure "query waited on a closed store" #endif