Skip to content

Commit

Permalink
Merge pull request #242 from cachix/socket-resource-vanished
Browse files Browse the repository at this point in the history
if socket raises an exception that resource vanished, consider it closed
  • Loading branch information
domenkozar committed Dec 27, 2023
2 parents 2f9d6bd + f18f3ab commit f238132
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
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.13,
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.13,
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.13,
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.13,
Expand Down

0 comments on commit f238132

Please sign in to comment.