]> granicus.if.org Git - python/commitdiff
bpo-31249: Fix ref cycle in ThreadPoolExecutor (#3178)
authorVictor Stinner <victor.stinner@gmail.com>
Tue, 22 Aug 2017 14:50:42 +0000 (16:50 +0200)
committerGitHub <noreply@github.com>
Tue, 22 Aug 2017 14:50:42 +0000 (16:50 +0200)
* bpo-31249: Fix ref cycle in ThreadPoolExecutor

concurrent.futures: WorkItem.run() used by ThreadPoolExecutor now
breaks a reference cycle between an exception object and the WorkItem
object. ThreadPoolExecutor.shutdown() now also clears its threads
set.

* shutdown() now only clears threads if wait is true.

* Revert changes on shutdown()

Lib/concurrent/futures/thread.py
Misc/NEWS.d/next/Library/2017-08-22-12-44-48.bpo-31249.STPbb9.rst [new file with mode: 0644]

index 1f0a1d4b977f316cdd8fa65dc57b3e4afb8204dd..0b5d5373ffdc0aa20b6b1717e5f8bc99d1e94f37 100644 (file)
@@ -54,8 +54,10 @@ class _WorkItem(object):
 
         try:
             result = self.fn(*self.args, **self.kwargs)
-        except BaseException as e:
-            self.future.set_exception(e)
+        except BaseException as exc:
+            self.future.set_exception(exc)
+            # Break a reference cycle with the exception 'exc'
+            self = None
         else:
             self.future.set_result(result)
 
diff --git a/Misc/NEWS.d/next/Library/2017-08-22-12-44-48.bpo-31249.STPbb9.rst b/Misc/NEWS.d/next/Library/2017-08-22-12-44-48.bpo-31249.STPbb9.rst
new file mode 100644 (file)
index 0000000..f11a668
--- /dev/null
@@ -0,0 +1,2 @@
+concurrent.futures: WorkItem.run() used by ThreadPoolExecutor now breaks a
+reference cycle between an exception object and the WorkItem object.