Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

if socket raises an exception that resource vanished, consider it closed #242

Merged
merged 1 commit into from
Dec 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/Network/WebSockets/Stream.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module Network.WebSockets.Stream

import Control.Concurrent.MVar (MVar, newEmptyMVar, newMVar,
putMVar, takeMVar, withMVar)
import Control.Exception (SomeException, SomeAsyncException, throwIO, catch, fromException)
import Control.Exception (SomeException, SomeAsyncException, throwIO, catch, try, fromException)
import Control.Monad (forM_)
import qualified Data.Attoparsec.ByteString as Atto
import qualified Data.Binary.Get as BIN
Expand All @@ -31,6 +31,7 @@ import qualified Network.Socket.ByteString.Lazy as SBL (sendAll)
#else
import qualified Network.Socket.ByteString as SB (sendAll)
#endif
import System.IO.Error (isResourceVanishedError)

import Network.WebSockets.Types

Expand Down Expand Up @@ -116,8 +117,13 @@ makeSocketStream :: S.Socket -> IO Stream
makeSocketStream socket = makeStream receive send
where
receive = do
bs <- SB.recv socket 8192
return $ if B.null bs then Nothing else Just bs
bs <- try $ SB.recv socket 8192
case bs of
-- If the resource vanished, the socket was closed
Left e | isResourceVanishedError e -> return Nothing
| otherwise -> throwIO e
Right bs' | B.null bs' -> return Nothing
| otherwise -> return $ Just bs'

send Nothing = return ()
send (Just bs) = do
Expand Down
8 changes: 4 additions & 4 deletions websockets.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ Library
Build-depends:
async >= 2.2 && < 2.3,
attoparsec >= 0.10 && < 0.15,
base >= 4.8 && < 5,
base >= 4.14 && < 5,
base64-bytestring >= 0.1 && < 1.3,
binary >= 0.8.1 && < 0.11,
bytestring >= 0.9 && < 0.12,
Expand Down Expand Up @@ -140,7 +140,7 @@ Test-suite websockets-tests
-- Copied from regular dependencies...
async >= 2.2 && < 2.3,
attoparsec >= 0.10 && < 0.15,
base >= 4 && < 5,
base >= 4.14 && < 5,
base64-bytestring >= 0.1 && < 1.3,
binary >= 0.8.1 && < 0.11,
bytestring >= 0.9 && < 0.12,
Expand Down Expand Up @@ -200,7 +200,7 @@ Executable websockets-autobahn
-- Copied from regular dependencies...
async >= 2.2 && < 2.3,
attoparsec >= 0.10 && < 0.15,
base >= 4 && < 5,
base >= 4.14 && < 5,
base64-bytestring >= 0.1 && < 1.3,
binary >= 0.8.1 && < 0.11,
bytestring >= 0.9 && < 0.12,
Expand All @@ -227,7 +227,7 @@ Benchmark bench-mask
-- Copied from regular dependencies...
async >= 2.2 && < 2.3,
attoparsec >= 0.10 && < 0.15,
base >= 4 && < 5,
base >= 4.14 && < 5,
base64-bytestring >= 0.1 && < 1.3,
binary >= 0.8.1 && < 0.11,
bytestring >= 0.9 && < 0.12,
Expand Down
Loading