[Python-checkins] bpo-31249: Fix ref cycle in ThreadPoolExecutor (#3178)

Victor Stinner webhook-mailer at python.org
Tue Aug 22 10:50:46 EDT 2017


https://github.com/python/cpython/commit/bc61315377056fe362b744d9c44e17cd3178ce54
commit: bc61315377056fe362b744d9c44e17cd3178ce54
branch: master
author: Victor Stinner <victor.stinner at gmail.com>
committer: GitHub <noreply at github.com>
date: 2017-08-22T16:50:42+02:00
summary:

bpo-31249: Fix ref cycle in ThreadPoolExecutor (#3178)

* 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()

files:
A Misc/NEWS.d/next/Library/2017-08-22-12-44-48.bpo-31249.STPbb9.rst
M Lib/concurrent/futures/thread.py

diff --git a/Lib/concurrent/futures/thread.py b/Lib/concurrent/futures/thread.py
index 1f0a1d4b977..0b5d5373ffd 100644
--- a/Lib/concurrent/futures/thread.py
+++ b/Lib/concurrent/futures/thread.py
@@ -54,8 +54,10 @@ def run(self):
 
         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
index 00000000000..f11a66802d1
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2017-08-22-12-44-48.bpo-31249.STPbb9.rst
@@ -0,0 +1,2 @@
+concurrent.futures: WorkItem.run() used by ThreadPoolExecutor now breaks a
+reference cycle between an exception object and the WorkItem object.



More information about the Python-checkins mailing list