module Specs.UseSpec whereimport Control.Concurrent.Async (race)import Data.Text qualified as Textimport Hasql.Decoders qualified as Decodersimport Hasql.Encoders qualified as Encodersimport Hasql.Errors qualified as Errorsimport Hasql.Poolimport Hasql.Pool.Config qualified as Configimport Hasql.Session qualified as Sessionimport Hasql.Statement qualified as Statementimport Helpers.Scripts qualified as Scriptsimport Helpers.Sessions qualified as Sessionsimport Preludeimport Test.Hspecspec :: SpecWith Scripts.ScopeParamsspec = do it "Releases a spot in the pool when there is a query error" \scopeParams -> Scripts.onAutotaggedPool 1 10 1_800 1_800 scopeParams \_ pool -> do use pool Sessions.badQuery `shouldNotReturn` (Right ()) use pool Sessions.selectOne `shouldReturn` (Right 1) it "Connection errors cause eviction of connection" \scopeParams -> Scripts.onAutotaggedPool 1 10 1_800 1_800 scopeParams \_ pool -> do _ <- use pool $ Sessions.closeConn >> Sessions.selectOne _ <- use pool $ Sessions.closeConn >> Sessions.selectOne _ <- use pool $ Sessions.closeConn >> Sessions.selectOne res <- use pool $ Sessions.selectOne shouldSatisfy res $ isRight it "Driver errors cause eviction of connection" \scopeParams -> do settingName <- Scripts.generateVarname Scripts.onAutotaggedPool 1 10 1_800 1_800 scopeParams \_ pool -> do use pool (Sessions.setSetting settingName "present") `shouldReturn` Right () result <- use pool driverError result `shouldSatisfy` \case Left (SessionUsageError (Errors.ConnectionUseError _)) -> True _ -> False use pool (Sessions.getSetting settingName) `shouldReturn` Right Nothing -- After a server restart or @pg_terminate_backend@ the first statement on -- the closed connection used to fail with a catchable -- 'Errors.UnexpectedRowCountStatementError' or server error instead of a -- 'Errors.ConnectionUseError'; hasql now classifies the loss as -- connection-fatal on every receive. Either way the pool must not hand -- the connection out again. it "Connections closed by the server cause eviction of connection" \scopeParams -> Scripts.onAutotaggedPool 1 10 1_800 1_800 scopeParams \appName pool -> do use pool Sessions.selectOne `shouldReturn` Right 1 Scripts.onAutotaggedPool 1 10 1_800 1_800 scopeParams \_ adminPool -> use adminPool (terminateBackends appName) `shouldReturn` Right [True] _ <- use pool Sessions.selectOne use pool Sessions.selectOne `shouldReturn` Right 1 it "Unexpected row counts on a healthy connection don't cause eviction of connection" \scopeParams -> Scripts.onAutotaggedPool 1 10 1_800 1_800 scopeParams \_ pool -> do Right pid <- use pool backendPid result <- use pool selectTwoRowsAsSingleRow result `shouldSatisfy` \case Left (SessionUsageError (Errors.SessionUseError (Errors.StatementSessionError _ _ _ _ _ Errors.UnexpectedRowCountStatementError {}))) -> True _ -> False use pool backendPid `shouldReturn` Right pid it "Connection gets returned to the pool after normal use" \scopeParams -> Scripts.onAutotaggedPool 1 10 1_800 1_800 scopeParams \_ pool -> do _ <- use pool $ Sessions.selectOne _ <- use pool $ Sessions.selectOne _ <- use pool $ Sessions.selectOne _ <- use pool $ Sessions.selectOne res <- use pool $ Sessions.selectOne shouldSatisfy res $ isRight it "Connection gets returned to the pool after non-connection error" \scopeParams -> Scripts.onAutotaggedPool 1 10 1_800 1_800 scopeParams \_ pool -> do _ <- use pool $ Sessions.badQuery _ <- use pool $ Sessions.badQuery _ <- use pool $ Sessions.badQuery _ <- use pool $ Sessions.badQuery res <- use pool $ Sessions.selectOne shouldSatisfy res $ isRight -- https://github.com/nikita-volkov/hasql-pool/issues/38 -- -- When a session is interrupted by an asynchronous exception (e.g., a -- caller-side timeout racing the query, as simulated here via `race`) -- while it is genuinely blocked waiting on the server's response, the -- underlying libpq connection is left mid-command: the query was sent, -- but its result was never read. `onLiveConn` in Hasql.Pool.use still -- unconditionally returns such a connection to the pool (the `Left exc` -- branch calls `returnConn` for all exceptions, not just synchronous -- ones), so the next `use` call hands out a connection whose protocol -- state is desynced from libpq's expectations. This is a plausible root -- cause of the "connection pointer is NULL" reports: two independent -- consumers of hasql-pool end up driving the same libpq connection state -- machine without coordination. it "Does not return a connection to the pool when the session is interrupted by an asynchronous exception" \scopeParams -> Scripts.onAutotaggedPool 1 10 1_800 1_800 scopeParams \_ pool -> do started <- newEmptyMVar _ <- race ( use pool do liftIO $ putMVar started () Sessions.sleep 2 ) ( do takeMVar started -- Give the query time to actually reach the server and for -- the client to start blocking on the socket read, as -- opposed to being cancelled while still sending. threadDelay 200_000 ) res <- use pool Sessions.selectOne res `shouldSatisfy` isRight it "Gives a freed connection to a pool request waiting for it rather than to a later one" \scopeParams -> Scripts.onAutotaggedPool 1 10 1_800 1_800 scopeParams \_ pool -> do order <- newIORef [] held <- newEmptyMVar done <- newEmptyMVar finished <- newEmptyMVar let record name = use pool (liftIO (atomicModifyIORef' order \names -> (names <> [name], ()))) -- Requests a connection again as soon as it returns one, before the -- waiting pool request gets to run. _ <- forkIO do _ <- use pool (liftIO (putMVar held () *> takeMVar done)) _ <- record "later" putMVar finished () takeMVar held _ <- forkIO (record "waiting" *> putMVar finished ()) -- Give the waiting pool request time to start waiting. threadDelay 100_000 putMVar done () replicateM_ 2 (takeMVar finished) readIORef order `shouldReturn` ["waiting", "later"] it "Loses no capacity when a pool request waiting for a connection is interrupted" \scopeParams -> Scripts.onAutotaggedPool 1 10 1_800 1_800 scopeParams \_ pool -> do held <- newEmptyMVar done <- newEmptyMVar _ <- forkIO (void (use pool (liftIO (putMVar held () *> takeMVar done)))) takeMVar held waiting <- forkIO (void (use pool Sessions.selectOne)) -- Give the pool request time to start waiting. threadDelay 100_000 killThread waiting putMVar done () use pool Sessions.selectOne `shouldReturn` Right 1 it "Reuses the most recently returned connection with LIFO" \scopeParams -> Scripts.onAutotaggedPoolWithReuseOrder 2 10 1_800 1_800 Config.LIFO scopeParams \_ pool -> do (first, second) <- returnTwoConnections pool first `shouldNotBe` second use pool Sessions.backendPid `shouldReturn` Right second it "Reuses the least recently returned connection by default" \scopeParams -> Scripts.onAutotaggedPool 2 10 1_800 1_800 scopeParams \_ pool -> do (first, second) <- returnTwoConnections pool first `shouldNotBe` second use pool Sessions.backendPid `shouldReturn` Right first it "Cached type errors cause eviction of connection" \scopeParams -> do typeName <- Text.replace "-" "_" <$> Scripts.generateName "cached_type_" Scripts.onAutotaggedPool 1 10 1_800 1_800 scopeParams \_ pool -> do use pool (Session.script (createTypeSql typeName)) `shouldReturn` Right () use pool (roundtripEnum typeName "ok") `shouldReturn` Right "ok" use pool (Session.script (recreateTypeSql typeName)) `shouldReturn` Right () res <- use pool (roundtripEnum typeName "ok") shouldSatisfy res \case Left (SessionUsageError _) -> True _ -> False use pool (roundtripEnum typeName "ok") `shouldReturn` Right "ok"quoteIdentifier :: Text -> TextquoteIdentifier identifier = "\"" <> Text.replace "\"" "\"\"" identifier <> "\""createTypeSql :: Text -> TextcreateTypeSql typeName = "create type " <> quotedTypeName <> " as enum ('sad', 'ok', 'happy')" where quotedTypeName = quoteIdentifier typeNamerecreateTypeSql :: Text -> TextrecreateTypeSql typeName = "drop type " <> quotedTypeName <> "; create type " <> quotedTypeName <> " as enum ('sad', 'ok', 'happy')" where quotedTypeName = quoteIdentifier typeNameroundtripEnum :: Text -> Text -> Session.Session TextroundtripEnum typeName value = Session.statement value statement where statement = Statement.preparable ("select $1 :: " <> quoteIdentifier typeName) (Encoders.param (Encoders.nonNullable (Encoders.enum Nothing typeName id))) (Decoders.singleRow (Decoders.column (Decoders.nonNullable (Decoders.enum Nothing typeName Just))))terminateBackends :: Text -> Session.Session [Bool]terminateBackends appName = Session.statement appName statement where statement = Statement.unpreparable "select pg_terminate_backend(pid, 10000) from pg_stat_activity where application_name = $1" (Encoders.param (Encoders.nonNullable Encoders.text)) (Decoders.rowList (Decoders.column (Decoders.nonNullable Decoders.bool)))backendPid :: Session.Session Int32backendPid = Session.statement () statement where statement = Statement.preparable "select pg_backend_pid()" Encoders.noParams (Decoders.singleRow (Decoders.column (Decoders.nonNullable Decoders.int4)))selectTwoRowsAsSingleRow :: Session.Session Int64selectTwoRowsAsSingleRow = Session.statement () statement where statement = Statement.preparable "select * from (values (1 :: int8), (2 :: int8)) as t" Encoders.noParams (Decoders.singleRow (Decoders.column (Decoders.nonNullable Decoders.int8)))driverError :: Session.Session ()driverError = Session.onLibpqConnection \connection -> pure (Left (Errors.ConnectionUseError (Errors.LocalConnectionError "synthetic driver error")), connection)-- | Establish two connections, return the first before the second, and give-- their backend PIDs.returnTwoConnections :: Pool -> IO (Int32, Int32)returnTwoConnections pool = do held <- newEmptyMVar done <- newEmptyMVar returned <- newEmptyMVar _ <- forkIO do res <- use pool (Sessions.backendPid <* liftIO (putMVar held () *> takeMVar done)) putMVar returned res takeMVar held res <- use pool do second <- Sessions.backendPid first <- liftIO do putMVar done () takeMVar returned pure (first, second) case res of Right (Right first, second) -> pure (first, second) _ -> fail ("Unexpected result: " <> show res)