mirror of
https://github.com/simplex-chat/simplexmq.git
synced 2026-11-22 19:58:39 +00:00
ntf server: prevent hanging on server stop (#2041)
* ntf server: prevent hanging on server stop * lib: fail database operations on a closed PostgreSQL store
This commit is contained in:
1 parent
f16343ef2a
commit
b85f2d54ed
2 files changed
+21
-6
No files matched your search
@@ -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
|
||||
|
||||
@@ -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
|
||||
Reference in new issue
Block a user