Repository: tornadoweb/tornado

Issue:
Possible memory leak
Hi,

I have memory leaks even in "hello, world" example listed here http://www.tornadoweb.org/en/stable/

Code of tornado_memory_test.py: http://pastebin.com/BfG7Na9Z

Here is an example of a problem:
$ python tornado_memory_test.py 
No memory leaks found!
No memory leaks found!
[I 161031 18:00:54 web:1946] 200 GET /test (127.0.0.1) 0.87ms
Memory garbage detected
[gc stat]: 73 objects in garbage
Memory garbage detected
[gc stat]: 73 objects in garbage
Memory garbage detected
[gc stat]: 73 objects in garbage
[I 161031 18:01:11 web:1946] 200 GET /test (127.0.0.1) 0.73ms
Memory garbage detected
[gc stat]: 146 objects in garbage


Here is a verbose output of garbage: http://pastebin.com/BzXudRXd

$ pip freeze | grep -i tor
tornado==4.4.2

$ python -V
Python 2.7.12

Also tried on:
$ pip freeze | grep tor
tornado==4.5.dev1

$ python -V
Python 2.7.12



Discussion:

Patch:
diff --git a/tornado/concurrent.py b/tornado/concurrent.py
index ec68dc4f..e7b0e44e 100644
--- a/tornado/concurrent.py
+++ b/tornado/concurrent.py
@@ -234,7 +234,10 @@ class Future(object):
         if self._result is not None:
             return self._result
         if self._exc_info is not None:
-            raise_exc_info(self._exc_info)
+            try:
+                raise_exc_info(self._exc_info)
+            finally:
+                self = None
         self._check_done()
         return self._result
 
diff --git a/tornado/gen.py b/tornado/gen.py
index d7df3b52..8e6a5940 100644
--- a/tornado/gen.py
+++ b/tornado/gen.py
@@ -315,6 +315,7 @@ def _make_coroutine_wrapper(func, replace_callback):
                     future.set_exc_info(sys.exc_info())
                 else:
                     _futures_to_runners[future] = Runner(result, future, yielded)
+                yielded = None
                 try:
                     return future
                 finally:
@@ -990,6 +991,7 @@ class Runner(object):
         # of the coroutine.
         self.stack_context_deactivate = None
         if self.handle_yield(first_yielded):
+            gen = result_future = first_yielded = None
             self.run()
 
     def register_callback(self, key):
@@ -1046,10 +1048,15 @@ class Runner(object):
                     except Exception:
                         self.had_exception = True
                         exc_info = sys.exc_info()
+                    future = None
 
                     if exc_info is not None:
-                        yielded = self.gen.throw(*exc_info)
-                        exc_info = None
+                        try:
+                            yielded = self.gen.throw(*exc_info)
+                        finally:
+                            # Break up a reference to itself
+                            # for faster GC on CPython.
+                            exc_info = None
                     else:
                         yielded = self.gen.send(value)
 
@@ -1082,6 +1089,7 @@ class Runner(object):
                     return
                 if not self.handle_yield(yielded):
                     return
+                yielded = None
         finally:
             self.running = False
 
@@ -1130,8 +1138,11 @@ class Runner(object):
                 self.future.set_exc_info(sys.exc_info())
 
         if not self.future.done() or self.future is moment:
+            def inner(f):
+                f = None
+                self.run()
             self.io_loop.add_future(
-                self.future, lambda f: self.run())
+                self.future, inner)
             return False
         return True
 
diff --git a/tornado/http1connection.py b/tornado/http1connection.py
index 7ee83161..464ca4d3 100644
--- a/tornado/http1connection.py
+++ b/tornado/http1connection.py
@@ -257,6 +257,7 @@ class HTTP1Connection(httputil.HTTPConnection):
             if need_delegate_close:
                 with _ExceptionLoggingContext(app_log):
                     delegate.on_connection_close()
+            header_future = None
             self._clear_callbacks()
         raise gen.Return(True)
 
diff --git a/tornado/util.py b/tornado/util.py
index d0f83d1f..186995f3 100644
--- a/tornado/util.py
+++ b/tornado/util.py
@@ -193,7 +193,11 @@ def exec_in(code, glob, loc=None):
 if PY3:
     exec("""
 def raise_exc_info(exc_info):
-    raise exc_info[1].with_traceback(exc_info[2])
+    try:
+        raise exc_info[1].with_traceback(exc_info[2])
+    finally:
+        exc_info = None
+
 """)
 else:
     exec("""


Test patch:
diff --git a/tornado/test/gen_test.py b/tornado/test/gen_test.py
index 8bbfc5fa..8102f223 100644
--- a/tornado/test/gen_test.py
+++ b/tornado/test/gen_test.py
@@ -992,6 +992,31 @@ class GenCoroutineTest(AsyncTestCase):
 
         self.finished = True
 
+    @skipNotCPython
+    def test_coroutine_refcounting(self):
+        # On CPython, tasks and their arguments should be released immediately
+        # without waiting for garbage collection.
+        @gen.coroutine
+        def inner():
+            class Foo(object):
+                pass
+            local_var = Foo()
+            self.local_ref = weakref.ref(local_var)
+            yield gen.coroutine(lambda: None)()
+            raise ValueError('Some error')
+
+        @gen.coroutine
+        def inner2():
+            try:
+                yield inner()
+            except ValueError:
+                pass
+
+        self.io_loop.run_sync(inner2, timeout=3)
+
+        self.assertIs(self.local_ref(), None)
+        self.finished = True
+
 
 class GenSequenceHandler(RequestHandler):
     @asynchronous