jaspervdj / websockets-snap

Snap integration for the websockets library
Other
33 stars 21 forks source link

ParseError {errorContexts = ["demandInput"], errorMessage = "not enough bytes"} #3

Open twittner opened 11 years ago

twittner commented 11 years ago

Given this test server:

module Main where

import Control.Exception (SomeException)
import Control.Monad
import Control.Monad.IO.Class
import Network.WebSockets
import Network.WebSockets.Snap
import Snap (quickHttpServe)

main :: IO ()
main = quickHttpServe (runWebSocketsSnap f)

f :: Request -> WebSockets Hybi10 ()
f r = do
    acceptRequest r
    catchWsError (forever $ void receiveDataMessage) printE

printE :: MonadIO m => SomeException -> m ()
printE = liftIO . print

and this example client.

Starting the server and the client establishes the connection just fine, but when the client receives a SIGINT the server logs:

an exception escaped to toplevel: ParseError {errorContexts = ["demandInput"], errorMessage = "not enough bytes"}

This happens only before the client has sent any data messages.

twittner commented 11 years ago

AFAICS this behavior is caused by runWebSocketsWith re-throwing iteratee exceptions. runWebSocketsSnapWith does not handle this, hence the exception reaches Snap. How about:

catchError (WS.runWebSocketsWith options' (fromSnapRequest rq) ws writeEnd)
           (const $ yield () EOF)

or some other safeguard mechanism in https://github.com/jaspervdj/websockets-snap/blob/0f62d70b5ee2d9f3d44fe0683f864df4088bf047/src/Network/WebSockets/Snap.hs#L33 ?