Tornado IOError "Stream closed" on request finish ()

I use tornado 2.0 and sometimes when I call self.finish () to end the asynchronous request, I get an IOError with the message "Stream is closed". It seems like this happens when the client finishes the request (i.e. by going to another page) before the server () call is completed. Is this the expected behavior and something I need for my code? I found this error a year ago, and this suggests that this is not what the client code should handle: https://github.com/facebook/tornado/issues/81 . This indicates an error in my code, and if so, what are the probable reasons?

Stacktrace:

Traceback (most recent call last): File "my_code.py", line 260, in my_method self.finish() File "/usr/lib/python2.6/site-packages/tornado/web.py", line 634, in finish self.request.finish() File "/usr/lib/python2.6/site-packages/tornado/httpserver.py", line 555, in finish self.connection.finish() File "/usr/lib/python2.6/site-packages/tornado/httpserver.py", line 349, in finish self._finish_request() File "/usr/lib/python2.6/site-packages/tornado/httpserver.py", line 372, in _finish_request self.stream.read_until(b("\r\n\r\n"), self._header_callback) File "/usr/lib/python2.6/site-packages/tornado/iostream.py", line 137, in read_until self._check_closed() File "/usr/lib/python2.6/site-packages/tornado/iostream.py", line 403, in _check_closed raise IOError("Stream is closed") IOError: Stream is closed 
+4
source share
1 answer

self.finish () is called to complete the asynchronous request, and some functions, such as self.render (), will call self.finish ().

If you call self.finish () after closing the connection, this will result in an error.

so you can check if you are calling some functions that terminate the connection before self.finish ()

or you can do the following:

 if not self._finished: #if the connection is closed, it won't call this function self.finish() else: pass 
0
source

All Articles