This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author m3rc1fulcameron
Recipients m3rc1fulcameron, paul.moore, steve.dower, tim.golden, zach.ware
Date 2019-09-16.17:51:36
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1568656297.2.0.121307750927.issue38188@roundup.psfhosted.org>
In-reply-to
Content
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:
    import _winapi
    log = logging.getLogger('')
    log.warning('Patching multiprocessing.reduction to deal with the _winapi.DuplicateHandle() PROCESS_DUP_HANDLE argument order bug.')
    class _PatchedDupHandle(object):
        '''Picklable wrapper for a handle.'''
        def __init__(self, handle, access, pid=None, options=0):
            if pid is None:
                # We just duplicate the handle in the current process and
                # let the receiving process steal the handle.
                pid = os.getpid()
            proc = _winapi.OpenProcess(_winapi.PROCESS_DUP_HANDLE, False, pid)
            try:
                self._handle = _winapi.DuplicateHandle(
                    _winapi.GetCurrentProcess(),
                    handle, proc, access, False, options)
            finally:
                _winapi.CloseHandle(proc)
            self._options = options
            self._access = access
            self._pid = pid

        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.
History
Date User Action Args
2019-09-16 17:51:37m3rc1fulcameronsetrecipients: + m3rc1fulcameron, paul.moore, tim.golden, zach.ware, steve.dower
2019-09-16 17:51:37m3rc1fulcameronsetmessageid: <1568656297.2.0.121307750927.issue38188@roundup.psfhosted.org>
2019-09-16 17:51:37m3rc1fulcameronlinkissue38188 messages
2019-09-16 17:51:36m3rc1fulcameroncreate