Skip to content

Commit

Permalink
fix: don't hide error on LISTEN channel failure (#3323)
Browse files Browse the repository at this point in the history
  • Loading branch information
steve-chavez committed Mar 12, 2024
1 parent 650249e commit 00f5780
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
- #3224, Return status code 406 for non-accepted media type instead of code 415 - @wolfgangwalther
- #3160, Fix using select= query parameter for custom media type handlers - @wolfgangwalther
- #3237, Dump media handlers and timezones with --dump-schema - @wolfgangwalther
- #3323, Don't hide error on LISTEN channel failure - @steve-chavez

### Deprecated

Expand Down
13 changes: 7 additions & 6 deletions src/PostgREST/AppState.hs
Original file line number Diff line number Diff line change
Expand Up @@ -487,15 +487,16 @@ listener appState observer = do
putIsListenerOn appState True
SQL.listen db $ SQL.toPgIdentifier dbChannel
SQL.waitForNotifications handleNotification db
_ ->
die $ "Could not listen for notifications on the " <> dbChannel <> " channel"
Left err -> do
observer $ DBListenerFail dbChannel err
exitFailure
where
handleFinally _ False _ = do
observer DBListenerFailNoRecoverObs
handleFinally dbChannel False err = do
observer $ DBListenerFailNoRecoverObs dbChannel err
killThread (getMainThreadId appState)
handleFinally dbChannel True _ = do
handleFinally dbChannel True err = do
-- if the thread dies, we try to recover
observer $ DBListenerFailRecoverObs dbChannel
observer $ DBListenerFailRecoverObs dbChannel err
putIsListenerOn appState False
-- assume the pool connection was also lost, call the connection worker
connectionWorker appState
Expand Down
20 changes: 14 additions & 6 deletions src/PostgREST/Observation.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module PostgREST.Observation

import qualified Data.ByteString.Lazy as LBS
import qualified Data.Text.Encoding as T
import qualified Hasql.Connection as SQL
import qualified Hasql.Pool as SQL
import qualified Network.Socket as NS
import Numeric (showFFloat)
Expand All @@ -36,8 +37,9 @@ data Observation
| ConnectionRetryObs Int
| ConnectionPgVersionErrorObs SQL.UsageError
| DBListenerStart Text
| DBListenerFailNoRecoverObs
| DBListenerFailRecoverObs Text
| DBListenerFail Text SQL.ConnectionError
| DBListenerFailNoRecoverObs Text (Either SomeException ())
| DBListenerFailRecoverObs Text (Either SomeException ())
| ConfigReadErrorObs
| ConfigReadErrorFatalObs SQL.UsageError Text
| ConfigReadErrorNotFatalObs SQL.UsageError
Expand Down Expand Up @@ -81,10 +83,12 @@ observationMessage = \case
jsonMessage usageErr
DBListenerStart channel -> do
"Listening for notifications on the " <> channel <> " channel"
DBListenerFailNoRecoverObs ->
"Automatic recovery disabled, exiting."
DBListenerFailRecoverObs channel ->
"Retrying listening for notifications on the " <> channel <> " channel.."
DBListenerFail channel err -> do
"Could not listen for notifications on the " <> channel <> " channel. " <> show err
DBListenerFailNoRecoverObs channel err ->
showListenerError err <> ". Automatic recovery disabled on the " <> channel <> " channel"
DBListenerFailRecoverObs channel err ->
showListenerError err <> ". Retrying listening for notifications on the " <> channel <> " channel.."
ConfigReadErrorObs ->
"An error ocurred when trying to query database settings for the config parameters"
ConfigReadErrorFatalObs usageErr hint ->
Expand All @@ -106,3 +110,7 @@ observationMessage = \case
showMillis x = toS $ showFFloat (Just 1) (x * 1000) ""

jsonMessage err = T.decodeUtf8 . LBS.toStrict . Error.errorPayload $ Error.PgError False err

showListenerError :: Either SomeException () -> Text
showListenerError (Left e) = show e
showListenerError (Right _) = "Failed getting notifications" -- this should not happen as the listener will never finish with a Right result

0 comments on commit 00f5780

Please sign in to comment.