Server crashes with broken pipe if client disconnects ungracefully?

Open
#150 23 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
linux, python

Research direction

Start at websocket.py's _write method around line 232 and review how an ungraceful client disconnect reaches socket.sendall. Check the issue's proposed BrokenPipeError handling and the surrounding 23-comment discussion; done means reconnecting after an abrupt tab close no longer crashes the server or emits the reported broken-pipe failure.

Written by the indexing model from the issue text.

Description

Hey, Love your library but having a little issue with cherrypy 3.3.0 on python 3.3.3 using chromium Version 31.0.1650.63 Debian jessie/sid (238485) on linux mint.

Basically, i can connect to the websocket, but if i close that tab and reopen it, it does not work. If I call socket.close in the client first, the problem dissapears. the server log shows a broken pipe error here in websocket.py around line 232:

def _write(self, b):
    """
    Trying to prevent a write operation
    on an already closed websocket stream.

    This cannot be bullet proof but hopefully
    will catch almost all use cases.
    """
    if self.terminated or self.sock is None:
        raise RuntimeError("Cannot send on a terminated websocket")
        self.sock.sendall(b)

I found a possible fix, I changed the function to:
def _write(self, b):
"""
Trying to prevent a write operation
on an already closed websocket stream.

    This cannot be bullet proof but hopefully
    will catch almost all use cases.
    """
    if self.terminated or self.sock is None:
        raise RuntimeError("Cannot send on a terminated websocket")
    try:
        self.sock.sendall(b)
    except BrokenPipeError:
        pass

And the error dissapears. I assume that ignoring the error is fine because we are closing the whole socket anyway, however I don't know much about the inner workings of ws4py.

Dominant language
Python
Stars
1.1k
Forks
285
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Lawouach/WebSocket-for-Python

All issues in Lawouach/WebSocket-for-Python

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.