]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Fix a subtle reference cycle that can lead to increased memory consumption.
authorBen Darnell <ben@bendarnell.com>
Sat, 30 Aug 2014 19:27:31 +0000 (15:27 -0400)
committerBen Darnell <ben@bendarnell.com>
Mon, 1 Sep 2014 14:45:01 +0000 (10:45 -0400)
Closes #1165.

tornado/gen.py

index 06f2715d6943f5971ddfe8df3034d421dac278b0..4bb82d422c7b48ab7c654e3556861ab6414398cb 100644 (file)
@@ -185,7 +185,18 @@ def _make_coroutine_wrapper(func, replace_callback):
                     future.set_exc_info(sys.exc_info())
                 else:
                     Runner(result, future, yielded)
-                return future
+                try:
+                    return future
+                finally:
+                    # Subtle memory optimization: if next() raised an exception,
+                    # the future's exc_info contains a traceback which
+                    # includes this stack frame.  This creates a cycle,
+                    # which will be collected at the next full GC but has
+                    # been shown to greatly increase memory usage of
+                    # benchmarks (relative to the refcount-based scheme
+                    # used in the absence of cycles).  We can avoid the
+                    # cycle by clearing the local variable after we return it.
+                    future = None
         future.set_result(result)
         return future
     return wrapper