Skip to content

Commit db25413

Browse files
ivanpaunohidmic
authored andcommitted
Remove is_winsock_handle() and instead test if wrapping the handle in a socket.socket() works (#494)
Signed-off-by: Ivan Santiago Paunovic <[email protected]>
1 parent b24d02c commit db25413

File tree

1 file changed

+10
-18
lines changed

1 file changed

+10
-18
lines changed

launch/launch/utilities/signal_management.py

+10-18
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
import asyncio
1818
import os
19-
import platform
2019
import signal
2120
import socket
2221
import threading
@@ -26,21 +25,10 @@
2625
from typing import Tuple # noqa: F401
2726
from typing import Union
2827

29-
30-
def is_winsock_handle(fd):
31-
"""Check if the given file descriptor is WinSock handle."""
32-
if platform.system() != 'Windows':
33-
return False
34-
try:
35-
# On Windows, WinSock handles and regular file handles
36-
# have disjoint APIs. This test leverages the fact that
37-
# attempting to get an MSVC runtime file handle from a
38-
# WinSock handle will fail.
39-
import msvcrt
40-
msvcrt.get_osfhandle(fd)
41-
return False
42-
except OSError:
43-
return True
28+
try:
29+
_WindowsError = WindowsError
30+
except NameError:
31+
_WindowsError = None
4432

4533

4634
class AsyncSafeSignalManager:
@@ -185,10 +173,14 @@ def __chain_wakeup_handle(self, wakeup_handle):
185173
if isinstance(prev_wakeup_handle, socket.socket):
186174
# Detach (Windows) socket and retrieve the raw OS handle.
187175
prev_wakeup_handle = prev_wakeup_handle.detach()
188-
if wakeup_handle != -1 and is_winsock_handle(wakeup_handle):
176+
if wakeup_handle != -1:
189177
# On Windows, os.write will fail on a WinSock handle. There is no WinSock API
190178
# in the standard library either. Thus we wrap it in a socket.socket instance.
191-
wakeup_handle = socket.socket(fileno=wakeup_handle)
179+
try:
180+
wakeup_handle = socket.socket(fileno=wakeup_handle)
181+
except _WindowsError as e:
182+
if e.winerror != 10038: # WSAENOTSOCK
183+
raise
192184
self.__prev_wakeup_handle = wakeup_handle
193185
return prev_wakeup_handle
194186

0 commit comments

Comments
 (0)