Search code examples
pythontornado

I wrote a project with tornado, but this exception is always in my log file


This is the error log:

[I 160308 11:09:59 web:1908] 200 GET /admin/realtime (117.93.180.216) 107.13ms [E 160308 11:09:59 http1connection:54] Uncaught exception Traceback (most recent call last): File "/usr/local/lib/python3.4/dist-packages/tornado/http1connection.py", line 238, in _read_message delegate.finish() File "/usr/local/lib/python3.4/dist-packages/tornado/httpserver.py", line 290, in finish self.delegate.finish() File "/usr/local/lib/python3.4/dist-packages/tornado/web.py", line 1984, in finish self.execute() File "/usr/local/lib/python3.4/dist-packages/blueware-1.0.10/blueware/hooks/framework_tornado/web.py", line 480, in _bw_wrapper__RequestDispatcher_execute future = wrapped(*args, **kwargs) File "/usr/local/lib/python3.4/dist-packages/tornado/web.py", line 2004, in execute **self.handler_kwargs) File "/usr/local/lib/python3.4/dist-packages/blueware-1.0.10/blueware/hooks/framework_tornado/web.py", line 448, in _bw_wrapper_RequestHandler___init___ return wrapped(*args, **kwargs) File "/usr/local/lib/python3.4/dist-packages/tornado/web.py", line 185, in init self.initialize(**kwargs) File "/usr/local/lib/python3.4/dist-packages/tornado/web.py", line 2714, in wrapper self.redirect(url) File "/usr/local/lib/python3.4/dist-packages/tornado/web.py", line 671, in redirect self.finish() File "/usr/local/lib/python3.4/dist-packages/blueware-1.0.10/blueware/hooks/framework_tornado/web.py", line 309, in _bw_wrapper_RequestHandler_finish_ return wrapped(*args, **kwargs) File "/usr/local/lib/python3.4/dist-packages/tornado/web.py", line 934, in finish self.flush(include_footers=True) File "/usr/local/lib/python3.4/dist-packages/tornado/web.py", line 870, in flush for transform in self._transforms: TypeError: 'NoneType' object is not iterable [I 160308 11:10:00 web:1908] 200 GET /admin/order?order_type=1&order_status=1&page=0&action=allreal (49.89.27.173) 134.53ms

Can anyone tell me how to solve this problem? Thank you very much


Solution

  • I assume that OneAPM (blueware agent) is compatible with your python and Tornado version, however it's can be tricky.

    Solution

    Move self.redirect(url) from your handler initialize method to get method, like this

    class MyHandler(tornado.web.RequestHandler):
        def get(self):
            self.redirect('/some_url')
    

    or use RedirectHandler.

    Every action that could finish request needs to be called in context of http-verb method (get, post, put and so on). The common mistake is making authetication/authorization in __init__ or initialize.

    More detail

    In Tornado's source there is a note about _transforms that is initialized in the constructor with None and set in_execute (oversimplifying - after headers_received).

    A transform modifies the result of an HTTP request (e.g., GZip encoding).

    Applications are not expected to create their own OutputTransforms or interact with them directly; the framework chooses which transforms (if any) to apply.

    Reproduce

    Sample that triggers this error. I'm including this only as a cross-check that blueware is not the cause:

    import tornado.ioloop
    import tornado.web
    
    class SomeHandler(tornado.web.RequestHandler):
    
        def initialize(self, *args, **kwargs):
            url = '/some'
            self.redirect(url) 
            # ^ this is wrong
    
        def get(self):
            # redirect should be here
            self.write("Hello")
    
    
    def make_app():
        return tornado.web.Application([
            (r"/", SomeHandler),
        ])
    
    if __name__ == "__main__":
        app = make_app()
        app.listen(8888)
        tornado.ioloop.IOLoop.current().start()
    

    And stacktrace:

    ERROR:tornado.application:Uncaught exception
    Traceback (most recent call last):
      File "/tmp/py3/lib/python3.4/site-packages/tornado/http1connection.py", line 238, in _read_message
        delegate.finish()
      File "/tmp/py3/lib/python3.4/site-packages/tornado/httpserver.py", line 289, in finish
        self.delegate.finish()
      File "/tmp/py3/lib/python3.4/site-packages/tornado/web.py", line 2022, in finish
        self.execute()
      File "/tmp/py3/lib/python3.4/site-packages/tornado/web.py", line 2042, in execute
        **self.handler_kwargs)
      File "/tmp/py3/lib/python3.4/site-packages/tornado/web.py", line 183, in __init__
        self.initialize(**kwargs)
      File "test.py", line 8, in initialize
        self.redirect(url)
      File "/tmp/py3/lib/python3.4/site-packages/tornado/web.py", line 666, in redirect
        self.finish()
      File "/tmp/py3/lib/python3.4/site-packages/tornado/web.py", line 932, in finish
        self.flush(include_footers=True)
      File "/tmp/py3/lib/python3.4/site-packages/tornado/web.py", line 868, in flush
        for transform in self._transforms:
    TypeError: 'NoneType' object is not iterable