Repository navigation
concurrent.futures ThreadPoolExecutor keeps unnecessary references to worker functions. #60488
Description
Activity
The ThreadPoolExecutor unnecessarily keeps references to _WorkItem objects. With the attached patch (which lacks a test), all tests still pass, and the references are removed as soon as they're no longer needed.
- addedtype-featureA feature request or enhancementA feature request or enhancement
on Oct 19, 2012 A new patch (with tests), and a fuller explanation:
At work, we've got Python talking to a customer's existing COM library; we're using Thomas Heller's 'comtypes' library to do that. Unfortunately, comtypes depends quite a lot on __del__-time cleanup, so reference counting matters. (I'm well aware that this isn't the recommended way to deal with resource cleanup in Python, but rewriting the existing infrastructure isn't a realistic option here.)
Anyway, it turned out that the concurrent.futures executors were indirectly holding onto references to COM objects, causing issues with our application.
The attached patch adds a few 'del' statements to remove references that are no longer needed. For the ProcessExecutor, some of those 'del' statements had to go into the multiprocessing.Queue implementation.
The troublesome pattern (in both multiprocessing and futures) takes the form (simplified):
def my_worker_function(...): ... while <exit_condition_not_satisfied>: obj = blocking_wait_for_next_item() do_processing(obj) ...
The issue is that the reference to obj is kept until the completion of the next blocking wait call. I'm suggesting just adding an extra 'del obj' after 'do_processing(obj)'.
- addedstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
on Oct 20, 2012 Sounds fine to me. You might want to make the test CPython-specific.
The concurrent.futures stuff looks good to me.
Could you add a comment explaining why the delete is necessary? And, as Antoine said, the test should be CPython only.
LGTM
Updated patch to execute tests only for CPython.
Added comments to patch
New changeset 70cef0a160cf by Andrew Svetlov in branch 'default':
Issue bpo-16284: Prevent keeping unnecessary references to worker functions in concurrent.futures ThreadPoolExecutor.
http://hg.python.org/cpython/rev/70cef0a160cfCommitted. Thanks.
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields: