-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
Incorrect Argument Order for Calls to _winapi.DuplicateHandle() in multiprocessing.reduction.DupHandle #82369
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
Comments
The DuplicateHandle function is utilized by the DupHandle object to duplicate handles for the purpose of sending and receiving between processes on Windows systems. At least on Python 3.7.3, this function is invoked with an incorrect argument order. In multiprocessing.reduction, send_handle passes _winapi.DUPLICATE_SAME_ACCESS as the access argument to the DupHandle constructor, which in-turn passes it to the access argument for _winapi.DuplicateHandle(). Instead, per https://docs.microsoft.com/en-us/windows/win32/api/handleapi/nf-handleapi-duplicatehandle this constant should be passed into the options argument. This bug results in any handles communicated via this method to have meaningless permissions set, which makes them unusable. I've monkeypatched the issue with the following code: try: def detach(self):
'''Get the handle. This should only be called once.'''
# retrieve handle from process which currently owns it
if self._pid == os.getpid():
# The handle has already been duplicated for this process.
return self._handle
# We must steal the handle from the process whose pid is self._pid.
proc = _winapi.OpenProcess(_winapi.PROCESS_DUP_HANDLE, False,
self._pid)
try:
return _winapi.DuplicateHandle(
proc, self._handle, _winapi.GetCurrentProcess(),
self._access, False, self._options|_winapi.DUPLICATE_CLOSE_SOURCE)
finally:
_winapi.CloseHandle(proc)
DupHandle = _PatchedDupHandle
def _patched_send_handle(conn, handle, destination_pid):
'''Send a handle over a local connection.'''
dh = DupHandle(handle, 0, destination_pid, _winapi.DUPLICATE_SAME_ACCESS)
conn.send(dh)
send_handle=_patched_send_handle
except ImportError:
pass The above seems to fix the problem on my machine by adding an additional options property to the DupHandle object and an options argument to send_handle function. |
As far as I can tell, reduction.send_handle isn't used internally in the Windows implementation, and it's also not a documented API function. However, it is tested on Windows in test_fd_transfer in Lib/test/_test_multiprocessing.py. As it turns out, the bug that Cameron's proposed solution fixes slips under the radar. By coincidence, DUPLICATE_SAME_ACCESS (2) has the same value as the file access right FILE_WRITE_DATA (2), and test_fd_transfer only checks whether the child can write to a file handle. I propose adding test_fd_transfer_windows to _TestConnection in Lib/test/_test_multiprocessing.py, which will test whether the parent and child are granted the same access to a kernel file object after the handle is sent to the child. @classmethod
def _check_handle_access(cls, conn):
handle = reduction.recv_handle(conn)
conn.send(get_handle_info(handle).GrantedAccess)
get_handle_info() and the required ctypes support definitions [1] would be defined at module scope as follows: if WIN32:
from ctypes import (WinDLL, WinError, Structure, POINTER,
byref, sizeof, c_void_p, c_ulong)
ntdll = WinDLL('ntdll')
ntdll.NtQueryObject.argtypes = (
c_void_p, # Handle
c_ulong, # ObjectInformationClass
c_void_p, # ObjectInformation
c_ulong, # ObjectInformationLength
POINTER(c_ulong)) # ReturnLength
ObjectBasicInformation = 0
class PUBLIC_OBJECT_BASIC_INFORMATION(Structure):
(r"https://docs.microsoft.com/en-us/windows/win32/api"
r"/winternl/nf-winternl-ntqueryobject")
_fields_ = (('Attributes', c_ulong),
('GrantedAccess', c_ulong),
('HandleCount', c_ulong),
('PointerCount', c_ulong),
('Reserved', c_ulong * 10))
def get_handle_info(handle):
info = PUBLIC_OBJECT_BASIC_INFORMATION()
status = ntdll.NtQueryObject(handle, ObjectBasicInformation,
byref(info), sizeof(info), None)
if status < 0:
error = ntdll.RtlNtStatusToDosError(status)
raise WinError(error)
return info [1] https://docs.microsoft.com/en-us/windows/win32/api/winternl/nf-winternl-ntqueryobject |
Let's make test_fd_transfer_windows a bit less hangy by polling for up to 60 seconds instead of simply trying to recv() and by terminating before trying to join().
|
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: