Skip to content

Commit

Permalink
Add workaround for send_handle bug on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelm committed Jul 29, 2024
1 parent f395f56 commit e7a8dbe
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 5 deletions.
78 changes: 78 additions & 0 deletions src/cutadapt/_handles.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
"""
Wrappers around multiprocessing.reduction.send_handle/recv_handle
that
- fix a bug in CPython’s standard library preventing send_handle from
working on Windows
- and translate between Windows handles and Unix file descriptors as necessary.
"""

import multiprocessing
import os

try:
import msvcrt
except ImportError:
msvcrt = None
try:
import _winapi
except ImportError:
_winapi = None


class _PatchedDupHandle:
"""Used by _patched_send_handle"""

def __init__(self, handle, access, pid=None, options=0):
if pid is None:
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._access = access
self._pid = pid

def detach(self):
if self._pid == os.getpid():
return self._handle
proc = _winapi.OpenProcess(_winapi.PROCESS_DUP_HANDLE, False, self._pid)
try:
return _winapi.DuplicateHandle(
proc,
self._handle,
_winapi.GetCurrentProcess(),
self._access,
False,
_winapi.DUPLICATE_CLOSE_SOURCE,
)
finally:
_winapi.CloseHandle(proc)


def _patched_send_handle(conn, handle, destination_pid):
"""
A patched version of multiprocessing.reduction.send_handle that works around
bug https://github.com/python/cpython/issues/82369
Adapted from code posted by Cameron Kennedy (m3rc1fulcameron) in that issue.
"""
dh = _PatchedDupHandle(handle, 0, destination_pid, _winapi.DUPLICATE_SAME_ACCESS)
conn.send(dh)


def send_handle(conn, fd, destination_pid):
if _winapi is None:
return multiprocessing.reduction.send_handle(conn, fd, destination_pid)
else:
handle = msvcrt.get_osfhandle(fd)
return _patched_send_handle(conn, handle, destination_pid)


def recv_handle(conn):
handle = multiprocessing.reduction.recv_handle(conn)
if msvcrt:
return msvcrt.open_osfhandle(handle, os.O_RDONLY | os.O_BINARY)
else:
return handle
8 changes: 3 additions & 5 deletions src/cutadapt/runners.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import dnaio

from ._handles import recv_handle, send_handle
from cutadapt.files import (
InputFiles,
OutputFiles,
Expand Down Expand Up @@ -85,8 +86,7 @@ def run(self):
with ExitStack() as stack:
try:
fds = [
multiprocessing.reduction.recv_handle(self._input_fds_pipe)
for _ in range(self._n_files)
recv_handle(self._input_fds_pipe) for _ in range(self._n_files)
]
self._input_fds_pipe.close()
raw_files = [
Expand Down Expand Up @@ -326,9 +326,7 @@ def __init__(
)
else:
with open(path, "rb") as f:
multiprocessing.reduction.send_handle(
input_fds_pipe_w, f.fileno(), pid
)
send_handle(input_fds_pipe_w, f.fileno(), pid)
input_fds_pipe_w.close()

file_format: Optional[FileFormat] = self._try_receive(file_format_connection_r)
Expand Down

0 comments on commit e7a8dbe

Please sign in to comment.