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()
This commit is contained in:
Victor Stinner 2017-08-22 16:50:42 +02:00 committed by GitHub
parent 5fe59f8e3a
commit bc61315377
2 changed files with 6 additions and 2 deletions

View File

@ -54,8 +54,10 @@ class _WorkItem(object):
try: try:
result = self.fn(*self.args, **self.kwargs) result = self.fn(*self.args, **self.kwargs)
except BaseException as e: except BaseException as exc:
self.future.set_exception(e) self.future.set_exception(exc)
# Break a reference cycle with the exception 'exc'
self = None
else: else:
self.future.set_result(result) self.future.set_result(result)

View File

@ -0,0 +1,2 @@
concurrent.futures: WorkItem.run() used by ThreadPoolExecutor now breaks a
reference cycle between an exception object and the WorkItem object.