Skip to content

bpo-33966, multiprocessing: Pool wait for worker start #7966

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions Lib/multiprocessing/pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def __repr__(self):


def worker(inqueue, outqueue, initializer=None, initargs=(), maxtasks=None,
wrap_exception=False):
wrap_exception=False, event=None):
if (maxtasks is not None) and not (isinstance(maxtasks, int)
and maxtasks >= 1):
raise AssertionError("Maxtasks {!r} is not valid".format(maxtasks))
Expand All @@ -104,6 +104,9 @@ def worker(inqueue, outqueue, initializer=None, initargs=(), maxtasks=None,
if initializer is not None:
initializer(*initargs)

if event is not None:
event.set()

completed = 0
while maxtasks is None or (maxtasks and completed < maxtasks):
try:
Expand Down Expand Up @@ -228,19 +231,31 @@ def _repopulate_pool(self):
"""Bring the number of pool processes up to the specified number,
for use after reaping workers which have exited.
"""
events = []
for i in range(self._processes - len(self._pool)):
event = self._ctx.Event()
events.append(event)
w = self.Process(target=worker,
args=(self._inqueue, self._outqueue,
self._initializer,
self._initargs, self._maxtasksperchild,
self._wrap_exception)
self._wrap_exception,
event)
)
self._pool.append(w)
w.name = w.name.replace('Process', 'PoolWorker')
w.daemon = True
w.start()
util.debug('added worker')

# bpo-33966: On Windows, if the worker is terminated too quickly,
# handles created by DupHandle() on reduction.dump() remain open in
# the parent process, causing a handles leak. Wait until all new
# workers started to make sure that the workers closed handles
# inherited by DupHandle.
for event in events:
event.wait()

def _maintain_pool(self):
"""Clean up any exited workers and start replacements for them.
"""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
multiprocess: On Windows, if a worker is terminated too quickly, handles
created by DupHandle() on reduction.dump() remain open in the parent process,
causing a handles leak. Pool now waits using events until all new workers
started to make sure that the workers closed handles inherited by DupHandle.