server: catch TimeoutThread and InvalidRequest exceptions in websocket connections

PR-URL: https://github.com/hasura/graphql-engine-mono/pull/8014
GitOrigin-RevId: eef2d1ce99ec87bb239584c3ca4a9f056131b5cc
This commit is contained in:
Krushan Bauva 2023-02-21 17:24:05 +05:30 committed by hasura-bot
parent 782c905bf4
commit 2b1d5be10a
2 changed files with 23 additions and 8 deletions

View File

@ -329,6 +329,7 @@ common lib-depends
, these
, time >= 1.9
, time-compat
, time-manager
, transformers
, transformers-base
, unordered-containers >= 0.2.12

View File

@ -66,11 +66,13 @@ import Hasura.Server.Prometheus
)
import ListT qualified
import Network.Wai.Extended (IpAddress)
import Network.Wai.Handler.Warp qualified as Warp
import Network.WebSockets qualified as WS
import Refined (unrefine)
import StmContainers.Map qualified as STMMap
import System.IO.Error qualified as E
import System.Metrics.Prometheus.Counter qualified as Prometheus.Counter
import System.TimeManager qualified as TM
newtype WSId = WSId {unWSId :: UUID.UUID}
deriving (Show, Eq, Hashable)
@ -343,14 +345,26 @@ createServerApp getMetricsConfig wsConnInitTimeout (WSServer logger@(L.Logger wr
messageHandler = _hOnMessage wsHandlers
closeHandler = _hOnClose wsHandlers
-- It's not clear what the unexpected exception handling story here should be. So at
-- least log properly and re-raise:
logUnexpectedExceptions = handle $ \(e :: SomeException) -> do
writeLog $
L.UnstructuredLog L.LevelError $
fromString $
"Unexpected exception raised in websocket. Please report this as a bug: " <> show e
throwIO e
logUnexpectedExceptions = flip catches handlers
where
handlers =
[ -- this exception occurs under the normal course of the web server running. Also fairly common during shutdowns.
-- Common suggestion is to gobble it.
-- Refer: https://hackage.haskell.org/package/warp-3.3.24/docs/src/Network.Wai.Handler.Warp.Settings.html#defaultShouldDisplayException
Handler $ \(_ :: TM.TimeoutThread) -> pure (),
Handler $ \(e :: Warp.InvalidRequest) -> do
writeLog $
L.UnstructuredLog L.LevelError $
fromString $
"Client exception: " <> show e
throwIO e,
Handler $ \(e :: SomeException) -> do
writeLog $
L.UnstructuredLog L.LevelError $
fromString $
"Unexpected exception raised in websocket. Please report this as a bug: " <> show e
throwIO e
]
shuttingDownReject =
WS.RejectRequest