WebSockets ucaught exception in #661

truhlikfredy opened this Issue Jan 6, 2013 · 3 comments

3 participants


I have ping application, so server side after receiving relies the data back. I think I get this error when I receive message and try to rely it back while the connection waits in the close handshake (or somewhere there, but around time when it closes the connection).

ERROR:root:Uncaught exception in /
Traceback (most recent call last):
  File "/usr/local/lib/python2.6/dist-packages/tornado-2.4.1-py2.6.egg/tornado/websocket.py", line 258, in wrapper
    return callback(*args, **kwargs)
  File "./t_ping.py-2.py", line 24, in on_message
  File "/usr/local/lib/python2.6/dist-packages/tornado-2.4.1-py2.6.egg/tornado/websocket.py", line 144, in write_message
    self.ws_connection.write_message(message, binary=binary)
  File "/usr/local/lib/python2.6/dist-packages/tornado-2.4.1-py2.6.egg/tornado/websocket.py", line 526, in write_message
    self._write_frame(True, opcode, message)
  File "/usr/local/lib/python2.6/dist-packages/tornado-2.4.1-py2.6.egg/tornado/websocket.py", line 516, in _write_frame
  File "/usr/local/lib/python2.6/dist-packages/tornado-2.4.1-py2.6.egg/tornado/iostream.py", line 202, in write
  File "/usr/local/lib/python2.6/dist-packages/tornado-2.4.1-py2.6.egg/tornado/iostream.py", line 571, in _check_closed
    raise IOError("Stream is closed")
IOError: Stream is closed

The server side source is:

#!/usr/bin/env python

import os.path
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop
from tornado.web import Application
from tornado.websocket import WebSocketHandler

import datetime

class Handler(WebSocketHandler):

    def open(self):
        print "New connection opened for user #:"+str(self.clients.index(self))
        IOLoop.instance().add_timeout(datetime.timedelta(seconds=5), self.test)

    def on_message(self, message):

    def on_close(self):
        print "Connection closed for user #"+str(self.clients.index(self))

    def test(self):

print "Server started."
HTTPServer(Application([("/", Handler)])).listen(8001)

The line number will not match because I removed comments and other stuff, but the line 24 was inside the on_message part.


So, considering self.write_message(message)
As per the traceback,
Stream doesn't exist by the time it tries to write the message which's may be obvious.
Can you check if you're accidentally terminating the stream ? somewhere in the code unless this has something to do with the timeout value ?


But it is normal to receive on_message event after the connection is closed? It is irrelevant of the time-out, that was just trying different things, but irrelevant to this problem. I think somewhere while closing it will get in some strange stage where it is partly closed and still not closed properly, and you still can sometimes recieve on_message even that the connection is in middle of closing it. The timeout what I meant was inside the tornado routines, there is the handshake timeout, could that have something to do with it?

tornadoweb member

If the client sends some data and then drops the connection, you'll get a chance to read the buffered data before the close callback is run, but writes will fail. I'll change this so the write will swallow the error and fail silently.

@bdarnell bdarnell added a commit that closed this issue Jun 6, 2013
@bdarnell bdarnell Catch StreamClosedErrors in WebSocketHandler and abort.
When the stream is closed with buffered data, the close callback won't
be run until all buffered data is consumed, but any attempt to write
to the stream will fail, as will reading past the end of the buffer.
This requires a try/except around each read or write, analogous to the
one introduced in HTTPServer in commit 3258726.

Closes #604.
Closes #661.
@bdarnell bdarnell closed this in 60693e0 Jun 6, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment