Issue #58 new

WebSocketError: Socket is dead

Vijay Reddy
created an issue

I just started using gevent-websocket and I tried to create a test websocket server. I followed one of the example and I getting the error "WebSocketError: Socket is dead" when a client disconnects (or refreshes):

Here is the code snippet:

import geventwebsocket from geventwebsocket import WebSocketServer, WebSocketApplication, Resource

class PlotApplication(WebSocketApplication): def on_open(self): for i in xrange(5): self.ws.send("0 %s %s\n" % (i, random.random())) gevent.sleep(0.1)

def on_close(self,reason):
    try:
        print "Connection Closed!!!", reason
    except geventwebsocket.WebSocketError, ex:
        print "ERROR: {0}: {1}".format(ex.__class__.__name__, ex)
    except:
        print "test else"

if name == "main": server = WebSocketServer( ('', 6996), Resource({ '^/.*': PlotApplication, }), debug=False ) server.serve_forever()

Please advise on how to fix this. It does not look like, it would call "on_close" function when the socket closes. I am using Python 2.7.7 on Debian.

Thanks

Comments (2)

  1. Vijay Reddy reporter

    I just copied example code from Chat server and it seems to be working fine for some reason. I am not sure why the above code would not work, but I will try to figure it out. Thank you.

  2. Joeri Muntjewerf

    This error is caused by an empty message variable. None is not an expected value and should be filtered out.

    def send_frame(self, message, opcode):
            """
            Send a frame over the websocket with message as its payload
            """
            if self.closed:
                self.current_app.on_close(MSG_ALREADY_CLOSED)
                raise WebSocketError(MSG_ALREADY_CLOSED)
    
            if opcode == self.OPCODE_TEXT:
                message = self._encode_bytes(message)
            elif opcode == self.OPCODE_BINARY:
                message = str(message)
    
            header = Header.encode_header(True, opcode, '', len(message), 0)
    
            try:
                self.raw_write(header + message)  # Fails if message was None
            except error:
                raise WebSocketError(MSG_SOCKET_DEAD)  # Labeled as as MSG_SOCKET_DEAD error while it actually isn't
    

    This fixed it for me :-)

    def send_frame(self, message, opcode):
            """
            Send a frame over the websocket with message as its payload
            """
        if message:  # Simple but fixed this specific error
    
            if self.closed:
                self.current_app.on_close(MSG_ALREADY_CLOSED)
                raise WebSocketError(MSG_ALREADY_CLOSED)
    
            if opcode == self.OPCODE_TEXT:
                message = self._encode_bytes(message)
            elif opcode == self.OPCODE_BINARY:
                message = str(message)
    
            header = Header.encode_header(True, opcode, '', len(message), 0)
    
            try:
                self.raw_write(header + message)
            except error:
                raise WebSocketError(MSG_SOCKET_DEAD) 
    
  3. Log in to comment