close websocket even if connection is broken #390

Closed
wants to merge 2 commits into
from

3 participants

@fredrocious

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

example:
https://gist.github.com/1318510

@mrjoes

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\iostream.py", line 301, in wrapper
callback(*args)
File "C:\Python26\lib\site-packages\tornado-2.1.1-py2.6.egg\tornado\websocket.py", line 307, in _on_length_indicator
self.close()
File "C:\Python26\lib\site-packages\tornado-2.1.1-py2.6.egg\tornado\websocket.py", line 324, in close
self.stream.write("\xff\x00")
File "C:\Python26\lib\site-packages\tornado-2.1.1-py2.6.egg\tornado\iostream.py", line 213, in write
self._check_closed()
File "C:\Python26\lib\site-packages\tornado-2.1.1-py2.6.egg\tornado\iostream.py", 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).

Thanks,
Serge.

@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