|
16 | 16 |
|
17 | 17 | import asyncio
|
18 | 18 | import os
|
19 |
| -import platform |
20 | 19 | import signal
|
21 | 20 | import socket
|
22 | 21 | import threading
|
|
26 | 25 | from typing import Tuple # noqa: F401
|
27 | 26 | from typing import Union
|
28 | 27 |
|
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 |
44 | 32 |
|
45 | 33 |
|
46 | 34 | class AsyncSafeSignalManager:
|
@@ -185,10 +173,14 @@ def __chain_wakeup_handle(self, wakeup_handle):
|
185 | 173 | if isinstance(prev_wakeup_handle, socket.socket):
|
186 | 174 | # Detach (Windows) socket and retrieve the raw OS handle.
|
187 | 175 | prev_wakeup_handle = prev_wakeup_handle.detach()
|
188 |
| - if wakeup_handle != -1 and is_winsock_handle(wakeup_handle): |
| 176 | + if wakeup_handle != -1: |
189 | 177 | # On Windows, os.write will fail on a WinSock handle. There is no WinSock API
|
190 | 178 | # 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 |
192 | 184 | self.__prev_wakeup_handle = wakeup_handle
|
193 | 185 | return prev_wakeup_handle
|
194 | 186 |
|
|
0 commit comments