From: Ben Darnell Date: Sat, 30 Aug 2014 19:27:31 +0000 (-0400) Subject: Fix a subtle reference cycle that can lead to increased memory consumption. X-Git-Tag: v4.0.2~4 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c1a123a8cd0b5de360e280ea34b8e97c078f2291;p=thirdparty%2Ftornado.git Fix a subtle reference cycle that can lead to increased memory consumption. Closes #1165. --- diff --git a/tornado/gen.py b/tornado/gen.py index 06f2715d6..4bb82d422 100644 --- a/tornado/gen.py +++ b/tornado/gen.py @@ -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