Skip to content

[WIP] bpo-33966, multiprocessing: Fix another handle leak #7965

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
7 changes: 5 additions & 2 deletions Lib/multiprocessing/popen_spawn_win32.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
WINSERVICE = sys.executable.lower().endswith("pythonservice.exe")


def _close_handles(*handles):
def _close_handles(handles):
for handle in handles:
_winapi.CloseHandle(handle)

Expand Down Expand Up @@ -66,8 +66,11 @@ def __init__(self, process_obj):
self.returncode = None
self._handle = hp
self.sentinel = int(hp)
# List of handles closed by the finalizer. DupHandle adds handles
# to this list on reduction.dump().
self._open_handles = [self.sentinel, int(rhandle)]
self.finalizer = util.Finalize(self, _close_handles,
(self.sentinel, int(rhandle)))
(self._open_handles,))

# send information to child
set_spawning_popen(self)
Expand Down
19 changes: 18 additions & 1 deletion Lib/multiprocessing/reduction.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,17 @@ def recv_handle(conn):
class DupHandle(object):
'''Picklable wrapper for a handle.'''
def __init__(self, handle, access, pid=None):
spawning = context.get_spawning_popen()
# bpo-33966: Keep the handle open in the parent process. The parent
# is responsible to close the handle when the child process
# completes.
#
# Fallback on calling DuplicateHandle() with DUPLICATE_CLOSE_SOURCE
# in the child process if popen_spawn_win32.Popen is not used. The
# fallback leaks an open handle in the parent process if the child
# process is terminated before closing the handle.
self._close_source = not(spawning is not None
and hasattr(spawning, '_open_handles'))
if pid is None:
# We just duplicate the handle in the current process and
# let the receiving process steal the handle.
Expand All @@ -113,6 +124,8 @@ def __init__(self, handle, access, pid=None):
self._handle = _winapi.DuplicateHandle(
_winapi.GetCurrentProcess(),
handle, proc, access, False, 0)
if not self._close_source:
spawning._open_handles.append(self._handle)
finally:
_winapi.CloseHandle(proc)
self._access = access
Expand All @@ -128,9 +141,13 @@ def detach(self):
proc = _winapi.OpenProcess(_winapi.PROCESS_DUP_HANDLE, False,
self._pid)
try:
if self._close_source:
flags = _winapi.DUPLICATE_CLOSE_SOURCE
else:
flags = 0
return _winapi.DuplicateHandle(
proc, self._handle, _winapi.GetCurrentProcess(),
self._access, False, _winapi.DUPLICATE_CLOSE_SOURCE)
self._access, False, flags)
finally:
_winapi.CloseHandle(proc)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
multiprocessing: Fix a handle leak when a pool worker is terminated quickly.

When using a pool of processes on Windows, if the worker is terminated
quickly, handles created by DupHandle() on reduction.dump() can remain open
in the parent process, causing a handles leak.

Use a different strategy in the case: keep the handle open in the parent
process for the lifetime of the worker, and the parent becomes responsible
to close the handle when the worker completes.