Skip to content

Commit

Permalink
warp: rethrowing asynchronous exceptions if caught.
Browse files Browse the repository at this point in the history
  • Loading branch information
kazu-yamamoto committed Nov 14, 2024
1 parent 7d4225e commit 9a436e3
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 3 deletions.
1 change: 1 addition & 0 deletions warp/Network/Wai/Handler/Warp/HTTP1.hs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ processRequest settings ii conn app th istatus src req mremainingRef idxhdr next
Right ResponseReceived -> return ()
Left (e :: SomeException)
| Just (ExceptionInsideResponseBody e') <- fromException e -> throwIO e'
| isAsyncException e -> throwIO e
| otherwise -> do
keepAlive <- sendErrorResponse settings ii conn th istatus req e
settingsOnException settings (Just req) e
Expand Down
2 changes: 1 addition & 1 deletion warp/Network/Wai/Handler/Warp/HTTP2.hs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ wrappedRecvN th slowlorisSize readN bufsize = do
return bs
where
handler :: E.SomeException -> IO ByteString
handler _ = return ""
handler = throughAsync (return "")

-- connClose must not be called here since Run:fork calls it
goaway :: Connection -> H2.ErrorCodeId -> ByteString -> IO ()
Expand Down
14 changes: 14 additions & 0 deletions warp/Network/Wai/Handler/Warp/Imports.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ module Network.Wai.Handler.Warp.Imports (
module Data.Word,
module Data.Maybe,
module Numeric,
throughAsync,
isAsyncException,
) where

import Control.Applicative
import Control.Exception
import Control.Monad
import Data.Bits
import Data.ByteString.Internal (ByteString (..))
Expand All @@ -23,3 +26,14 @@ import Data.Monoid
import Data.Ord
import Data.Word
import Numeric

isAsyncException :: Exception e => e -> Bool
isAsyncException e =
case fromException (toException e) of
Just (SomeAsyncException _) -> True
Nothing -> False

throughAsync :: IO a -> SomeException -> IO a
throughAsync action (SomeException e)
| isAsyncException e = throwIO e
| otherwise = action
4 changes: 2 additions & 2 deletions warp/Network/Wai/Handler/Warp/Run.hs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ socketConnection _ s = do
else settingsGracefulCloseTimeout1 set
if tm == 0
then close s
else gracefulClose s tm `E.catch` \(E.SomeException _) -> return ()
else gracefulClose s tm `E.catch` throughAsync (return ())
#else
, connClose = close s
#endif
Expand Down Expand Up @@ -179,7 +179,7 @@ runSettingsSocket set@Settings{settingsAccept = accept'} socket app = do
(s, sa) <- accept' socket
setSocketCloseOnExec s
-- NoDelay causes an error for AF_UNIX.
setSocketOption s NoDelay 1 `E.catch` \(E.SomeException _) -> return ()
setSocketOption s NoDelay 1 `E.catch` throughAsync (return ())
conn <- socketConnection set s
return (conn, sa)

Expand Down

0 comments on commit 9a436e3

Please sign in to comment.