Skip to content

Commit 7b5856e

Browse files
authored
Revert "bpo-33929: multiprocessing: fix handle leak on race condition (GH-7921)" (GH-7963)
This reverts commit 8b1ebcd.
1 parent 8b1ebcd commit 7b5856e

File tree

4 files changed

+6
-34
lines changed

4 files changed

+6
-34
lines changed

Lib/multiprocessing/popen_spawn_win32.py

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,6 @@
1818
WINEXE = (sys.platform == 'win32' and getattr(sys, 'frozen', False))
1919
WINSERVICE = sys.executable.lower().endswith("pythonservice.exe")
2020

21-
22-
def _close_handles(*handles):
23-
for handle in handles:
24-
_winapi.CloseHandle(handle)
25-
26-
2721
#
2822
# We define a Popen class similar to the one from subprocess, but
2923
# whose constructor takes a process object as its argument.
@@ -38,12 +32,8 @@ class Popen(object):
3832
def __init__(self, process_obj):
3933
prep_data = spawn.get_preparation_data(process_obj._name)
4034

41-
# read end of pipe will be duplicated by the child process
35+
# read end of pipe will be "stolen" by the child process
4236
# -- see spawn_main() in spawn.py.
43-
#
44-
# bpo-33929: Previously, the read end of pipe was "stolen" by the child
45-
# process, but it leaked a handle if the child process had been
46-
# terminated before it could steal the handle from the parent process.
4737
rhandle, whandle = _winapi.CreatePipe(None, 0)
4838
wfd = msvcrt.open_osfhandle(whandle, 0)
4939
cmd = spawn.get_command_line(parent_pid=os.getpid(),
@@ -66,8 +56,7 @@ def __init__(self, process_obj):
6656
self.returncode = None
6757
self._handle = hp
6858
self.sentinel = int(hp)
69-
self.finalizer = util.Finalize(self, _close_handles,
70-
(self.sentinel, int(rhandle)))
59+
self.finalizer = util.Finalize(self, _winapi.CloseHandle, (self.sentinel,))
7160

7261
# send information to child
7362
set_spawning_popen(self)

Lib/multiprocessing/reduction.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,12 @@ def dump(obj, file, protocol=None):
6868
__all__ += ['DupHandle', 'duplicate', 'steal_handle']
6969
import _winapi
7070

71-
def duplicate(handle, target_process=None, inheritable=False,
72-
*, source_process=None):
71+
def duplicate(handle, target_process=None, inheritable=False):
7372
'''Duplicate a handle. (target_process is a handle not a pid!)'''
74-
current_process = _winapi.GetCurrentProcess()
75-
if source_process is None:
76-
source_process = current_process
7773
if target_process is None:
78-
target_process = current_process
74+
target_process = _winapi.GetCurrentProcess()
7975
return _winapi.DuplicateHandle(
80-
source_process, handle, target_process,
76+
_winapi.GetCurrentProcess(), handle, target_process,
8177
0, inheritable, _winapi.DUPLICATE_SAME_ACCESS)
8278

8379
def steal_handle(source_pid, handle):

Lib/multiprocessing/spawn.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,7 @@ def spawn_main(pipe_handle, parent_pid=None, tracker_fd=None):
9696
assert is_forking(sys.argv), "Not forking"
9797
if sys.platform == 'win32':
9898
import msvcrt
99-
import _winapi
100-
101-
if parent_pid is not None:
102-
source_process = _winapi.OpenProcess(
103-
_winapi.PROCESS_DUP_HANDLE, False, parent_pid)
104-
else:
105-
source_process = None
106-
new_handle = reduction.duplicate(pipe_handle,
107-
source_process=source_process)
99+
new_handle = reduction.steal_handle(parent_pid, pipe_handle)
108100
fd = msvcrt.open_osfhandle(new_handle, os.O_RDONLY)
109101
else:
110102
from . import semaphore_tracker

Misc/NEWS.d/next/Library/2018-06-26-02-09-18.bpo-33929.OcCLah.rst

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)