From c29c35d7841e2a596bb59a400c9dca88fd29301d Mon Sep 17 00:00:00 2001 From: Ivan Santiago Paunovic Date: Fri, 26 Mar 2021 16:08:19 -0300 Subject: [PATCH 1/2] Remove is_winsock_handle() and instead test if wrapping the handle in a socket.socket() Signed-off-by: Ivan Santiago Paunovic --- launch/launch/utilities/signal_management.py | 27 ++++++++------------ 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/launch/launch/utilities/signal_management.py b/launch/launch/utilities/signal_management.py index 7323c5a60..040d86f66 100644 --- a/launch/launch/utilities/signal_management.py +++ b/launch/launch/utilities/signal_management.py @@ -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: @@ -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 From 7019425c0936e3045be297537b6d37a1d0d819c8 Mon Sep 17 00:00:00 2001 From: Ivan Santiago Paunovic Date: Fri, 26 Mar 2021 16:20:51 -0300 Subject: [PATCH 2/2] flake8 Signed-off-by: Ivan Santiago Paunovic --- launch/launch/utilities/signal_management.py | 1 - 1 file changed, 1 deletion(-) diff --git a/launch/launch/utilities/signal_management.py b/launch/launch/utilities/signal_management.py index 040d86f66..3aedc77c6 100644 --- a/launch/launch/utilities/signal_management.py +++ b/launch/launch/utilities/signal_management.py @@ -16,7 +16,6 @@ import asyncio import os -import platform import signal import socket import threading