Skip to content
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

Remove is_winsock_handle() and instead test if wrapping the handle in a socket.socket() works #494

Merged
merged 2 commits into from
Mar 29, 2021
Merged
Changes from 1 commit
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
27 changes: 10 additions & 17 deletions launch/launch/utilities/signal_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,10 @@
from typing import Tuple # noqa: F401
from typing import Union


def is_winsock_handle(fd):
"""Check if the given file descriptor is WinSock handle."""
if platform.system() != 'Windows':
return False
try:
# On Windows, WinSock handles and regular file handles
# have disjoint APIs. This test leverages the fact that
# attempting to get an MSVC runtime file handle from a
# WinSock handle will fail.
import msvcrt
msvcrt.get_osfhandle(fd)
return False
except OSError:
return True
try:
_WindowsError = WindowsError
except NameError:
_WindowsError = None


class AsyncSafeSignalManager:
Expand Down Expand Up @@ -185,10 +174,14 @@ def __chain_wakeup_handle(self, wakeup_handle):
if isinstance(prev_wakeup_handle, socket.socket):
# Detach (Windows) socket and retrieve the raw OS handle.
prev_wakeup_handle = prev_wakeup_handle.detach()
if wakeup_handle != -1 and is_winsock_handle(wakeup_handle):
if wakeup_handle != -1:
# On Windows, os.write will fail on a WinSock handle. There is no WinSock API
# in the standard library either. Thus we wrap it in a socket.socket instance.
wakeup_handle = socket.socket(fileno=wakeup_handle)
try:
wakeup_handle = socket.socket(fileno=wakeup_handle)
except _WindowsError as e:
if e.winerror != 10038: # WSAENOTSOCK
raise
self.__prev_wakeup_handle = wakeup_handle
return prev_wakeup_handle

Expand Down