New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
multiprocessing Pool keeps objects (tasks, args, results) alive too long #74047
Comments
The various workers in multiprocessing.Pool keep a reference to the last encountered task or task result. This means some data may be kept alive even after the caller is done with them, as long as some other task doesn't clobber the relevant variables. Specifically, Pool._handle_tasks(), Pool._handle_results() and the toplevel worker() function fail to clear references at the end of each loop. Originally reported at dask/distributed#956 |
Quick patch below. I'll make a PR once I have time to :-) diff --git a/Lib/multiprocessing/pool.py b/Lib/multiprocessing/pool.py
index ffdf426..945afa2 100644
--- a/Lib/multiprocessing/pool.py
+++ b/Lib/multiprocessing/pool.py
@@ -128,6 +128,8 @@ def worker(inqueue, outqueue, initializer=None, initargs=(), maxtasks=None,
util.debug("Possible encoding error while sending result: %s" % (
wrapped))
put((job, i, (False, wrapped)))
+
+ task = job = result = func = args = kwds = None
completed += 1
util.debug('worker exiting after %d tasks' % completed)
@@ -402,6 +404,8 @@ class Pool(object):
if set_length:
util.debug('doing set_length()')
set_length(i+1)
+ finally:
+ task = taskseq = job = None
else:
util.debug('task handler got sentinel')
@@ -445,6 +449,7 @@ class Pool(object):
cache[job]._set(i, obj)
except KeyError:
pass
+ task = job = obj = None
while cache and thread._state != TERMINATE:
try:
@@ -461,6 +466,7 @@ class Pool(object):
cache[job]._set(i, obj)
except KeyError:
pass
+ task = job = obj = None
if hasattr(outqueue, '_reader'):
util.debug('ensuring that outqueue is not full') |
Hi, Antoine, after this change, I sometimes see tests fail for 3.5 branch, for example http://buildbot.python.org/all/builders/x86%20Ubuntu%20Shared%203.5/builds/194/steps/test/logs/stdio. |
I can't reproduce here, on Ubuntu 16.04, after running the test 500 times. |
Ok, I can reproduce now. |
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:
The text was updated successfully, but these errors were encountered: