close websocket even if connection is broken #390

wants to merge 2 commits into

3 participants


if iostream is already closed, websocket will never close properly since currently close will always attempt to write websocket close frame marker



I have similar problem: if websocket connection is terminated forcibly (tcp connection was terminated), Tornado fails to gracefully shutdown this websocket.
It blows up with IOError when trying to send '\xff\x00' over closed connection and never calls on_close method of the websocket handler, so application does not have a chance to remove its timeouts, etc.

Here's stack trace that I'm seeing for one of the websocket protocols:
Traceback (most recent call last):
File "C:\Python26\lib\site-packages\tornado-2.1.1-py2.6.egg\tornado\", line 301, in wrapper
File "C:\Python26\lib\site-packages\tornado-2.1.1-py2.6.egg\tornado\", line 307, in _on_length_indicator
File "C:\Python26\lib\site-packages\tornado-2.1.1-py2.6.egg\tornado\", line 324, in close"\xff\x00")
File "C:\Python26\lib\site-packages\tornado-2.1.1-py2.6.egg\tornado\", line 213, in write
File "C:\Python26\lib\site-packages\tornado-2.1.1-py2.6.egg\tornado\", line 504, in _check_closed
raise IOError("Stream is closed")
IOError: Stream is closed

It happens because it expects two phase close handshake and does not expect that stream is closed already.

Provided fix will hide the issue, but not resolve it (stream will still raise IOError when client closes connection and then after N seconds will close the stream).


@bdarnell bdarnell closed this in ea0291b Jan 22, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment