Memory leak/no exception on send over closed socket

Greg Taylor avatarGreg Taylor created an issue

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

https://github.com/dliverman/eve-market-ping-map/blob/master/ws_server.py

The problem is down at line 165:

https://github.com/dliverman/eve-market-ping-map/blob/master/ws_server.py#L165

# 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

    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

    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

    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/handler.py:184]:
    WebSocket request accepted, switching protocols
    --------------------------------------------------------------------------------
    WebSocketError: Socket is dead
    --------------------------------------------------------------------------------
    DEBUG in websocket [/Users/user/Code/gevent-websocket/geventwebsocket/websocket.py:339]:
    Failed to write closing frame, closing socket
    --------------------------------------------------------------------------------
    --------------------------------------------------------------------------------
    DEBUG in websocket [/Users/user/Code/gevent-websocket/geventwebsocket/websocket.py:341]:
    Closing WebSocket
    
  5. Log in to comment
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.