Issue #25 resolved

Memory leak/no exception on send over closed socket

Greg Taylor
created an issue

I've got this simple gevent+websocket server running with pyzmq:

The problem is down at line 165:



This will block until messages arrive.

system_id = subscriber.recv()

Send the message

ws.send(system_id) }}} This blocks while waiting for data to come down the pipe. When we get it, the next line down sends said data out over websockets.

The problem is, it seems like even if someone disconnects/closes their browser, as soon as we receive data and hit ws.send(), it pretends like everything is OK, and doesn't appear to raise an Exception to let me know that the socket is closed, and I need to kill the greenlet.

This means that I gradually accumulate unclosed socket objects as people visit the site, causing my gradually run out of memory.

I'm stumped, and would hate to have to write this in Twisted, since this looks so much nicer. Any ideas would be awesome.

Comments (14)

  1. Daniele Varrazzo

    Hello, I think I have the same problem. This issue was created several months ago and got no answer. Jeffrey, do you have an answer for it? "it should work", or "it will never work"... anything? Thank you.

  2. Greg Taylor reporter

    Wonderful, will see if I can give this a shot tonight. Looked like there were some other changes, will have to see if there are any backwards incompatibilities.

  3. Greg Taylor reporter

    It looks like this is still leaking memory like crazy. The problem is that I get no exception when I attempt to send() on a socket that is no longer open, so none of my gevent workers ever stop blocking on recv() (they think the connection is still open).

    If I could get an exception raised when trying to send() to a close socket, this problem goes away.

  4. Jeffrey Gelens repo owner

    My local test case is working. If it's still not working in your code you're probably hitting some edge case we'll have to debug.

    DEBUG in handler [/Users/user/Code/gevent-websocket/geventwebsocket/]:
    WebSocket request accepted, switching protocols
    WebSocketError: Socket is dead
    DEBUG in websocket [/Users/user/Code/gevent-websocket/geventwebsocket/]:
    Failed to write closing frame, closing socket
    DEBUG in websocket [/Users/user/Code/gevent-websocket/geventwebsocket/]:
    Closing WebSocket
  5. Log in to comment