Commit 358fc87f authored by Victor Stinner's avatar Victor Stinner Committed by GitHub

Revert "[2.7] bpo-34172: multiprocessing.Pool leaks resources after being...

Revert "[2.7] bpo-34172: multiprocessing.Pool leaks resources after being deleted (GH-9686)" (GH-10970)

This reverts commit 4a7dd30f.
parent 40ef5b73
...@@ -162,9 +162,7 @@ class Pool(object): ...@@ -162,9 +162,7 @@ class Pool(object):
self._worker_handler = threading.Thread( self._worker_handler = threading.Thread(
target=Pool._handle_workers, target=Pool._handle_workers,
args=(self._cache, self._processes, self._pool, self.Process, args=(self, )
self._inqueue, self._outqueue, self._initializer,
self._initargs, self._maxtasksperchild, self._taskqueue)
) )
self._worker_handler.daemon = True self._worker_handler.daemon = True
self._worker_handler._state = RUN self._worker_handler._state = RUN
...@@ -196,56 +194,42 @@ class Pool(object): ...@@ -196,56 +194,42 @@ class Pool(object):
exitpriority=15 exitpriority=15
) )
@staticmethod def _join_exited_workers(self):
def _join_exited_workers(pool):
"""Cleanup after any worker processes which have exited due to reaching """Cleanup after any worker processes which have exited due to reaching
their specified lifetime. Returns True if any workers were cleaned up. their specified lifetime. Returns True if any workers were cleaned up.
""" """
cleaned = False cleaned = False
for i in reversed(range(len(pool))): for i in reversed(range(len(self._pool))):
worker = pool[i] worker = self._pool[i]
if worker.exitcode is not None: if worker.exitcode is not None:
# worker exited # worker exited
debug('cleaning up worker %d' % i) debug('cleaning up worker %d' % i)
worker.join() worker.join()
cleaned = True cleaned = True
del pool[i] del self._pool[i]
return cleaned return cleaned
def _repopulate_pool(self): def _repopulate_pool(self):
return self._repopulate_pool_static(self._processes, self._pool,
self.Process, self._inqueue,
self._outqueue, self._initializer,
self._initargs,
self._maxtasksperchild)
@staticmethod
def _repopulate_pool_static(processes, pool, Process, inqueue, outqueue,
initializer, initargs, maxtasksperchild):
"""Bring the number of pool processes up to the specified number, """Bring the number of pool processes up to the specified number,
for use after reaping workers which have exited. for use after reaping workers which have exited.
""" """
for i in range(processes - len(pool)): for i in range(self._processes - len(self._pool)):
w = Process(target=worker, w = self.Process(target=worker,
args=(inqueue, outqueue, args=(self._inqueue, self._outqueue,
initializer, self._initializer,
initargs, maxtasksperchild) self._initargs, self._maxtasksperchild)
) )
pool.append(w) self._pool.append(w)
w.name = w.name.replace('Process', 'PoolWorker') w.name = w.name.replace('Process', 'PoolWorker')
w.daemon = True w.daemon = True
w.start() w.start()
debug('added worker') debug('added worker')
@staticmethod def _maintain_pool(self):
def _maintain_pool(processes, pool, Process, inqueue, outqueue,
initializer, initargs, maxtasksperchild):
"""Clean up any exited workers and start replacements for them. """Clean up any exited workers and start replacements for them.
""" """
if Pool._join_exited_workers(pool): if self._join_exited_workers():
Pool._repopulate_pool_static(processes, pool, Process, inqueue, self._repopulate_pool()
outqueue, initializer, initargs,
maxtasksperchild)
def _setup_queues(self): def _setup_queues(self):
from .queues import SimpleQueue from .queues import SimpleQueue
...@@ -335,18 +319,16 @@ class Pool(object): ...@@ -335,18 +319,16 @@ class Pool(object):
return result return result
@staticmethod @staticmethod
def _handle_workers(cache, processes, pool, Process, inqueue, outqueue, def _handle_workers(pool):
initializer, initargs, maxtasksperchild, taskqueue):
thread = threading.current_thread() thread = threading.current_thread()
# Keep maintaining workers until the cache gets drained, unless the pool # Keep maintaining workers until the cache gets drained, unless the pool
# is terminated. # is terminated.
while thread._state == RUN or (cache and thread._state != TERMINATE): while thread._state == RUN or (pool._cache and thread._state != TERMINATE):
Pool._maintain_pool(processes, pool, Process, inqueue, outqueue, pool._maintain_pool()
initializer, initargs, maxtasksperchild)
time.sleep(0.1) time.sleep(0.1)
# send sentinel to stop workers # send sentinel to stop workers
taskqueue.put(None) pool._taskqueue.put(None)
debug('worker handler exiting') debug('worker handler exiting')
@staticmethod @staticmethod
......
...@@ -1359,13 +1359,6 @@ class _TestPool(BaseTestCase): ...@@ -1359,13 +1359,6 @@ class _TestPool(BaseTestCase):
# they were released too. # they were released too.
self.assertEqual(CountedObject.n_instances, 0) self.assertEqual(CountedObject.n_instances, 0)
def test_del_pool(self):
p = self.Pool(1)
wr = weakref.ref(p)
del p
gc.collect()
self.assertIsNone(wr())
def unpickleable_result(): def unpickleable_result():
return lambda: 42 return lambda: 42
......
Fix a reference issue inside multiprocessing.Pool that caused the pool to remain alive if it was deleted without being closed or terminated explicitly.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment