Skip to content

Commit f100b6d

Browse files
Fix gevent socket patching: use get_original with correct two-arg API
gevent.monkey.get_original() requires both module name and item name. Simplify by always importing socket normally for constants, and only getting the original unpatched socket class for connections. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 077f906 commit f100b6d

1 file changed

Lines changed: 11 additions & 9 deletions

File tree

similar.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,21 +42,23 @@
4242
# We obtain the original, non-patched socket module so that our calls
4343
# to the similarity server are truly blocking.
4444

45+
import socket
46+
4547
if TYPE_CHECKING:
46-
import socket
48+
_original_socket = socket.socket
4749
else:
4850
try:
49-
# gevent: get the original stdlib socket module
50-
from gevent import monkey as _monkey # type: ignore
51-
socket = _monkey.get_original("socket") # type: ignore
51+
# gevent: get the original, non-patched socket class
52+
from gevent.monkey import get_original # type: ignore
53+
_original_socket = get_original("socket", "socket")
5254
except ImportError:
5355
try:
54-
# eventlet: get the original stdlib socket module
56+
# eventlet: get the original, non-patched socket class
5557
import eventlet # type: ignore
56-
socket = eventlet.patcher.original("socket") # type: ignore
58+
_original_socket = eventlet.patcher.original("socket").socket # type: ignore
5759
except ImportError:
58-
# No async worker: use the stdlib socket directly
59-
import socket
60+
# No async worker: stdlib socket is already the original
61+
_original_socket = socket.socket
6062

6163
# The following two functions replicate and hack/tweak corresponding functions
6264
# from multiprocessing.connection. This is necessary because the original
@@ -68,7 +70,7 @@
6870

6971
def _SocketClient(address: Address) -> Connection:
7072
"""Return a connection object connected to the socket given by `address`"""
71-
with closing(socket.socket(socket.AF_INET)) as s:
73+
with closing(_original_socket(socket.AF_INET)) as s:
7274
s.setblocking(True)
7375
s.connect(address)
7476
# The following cast() hack is required since Connection()

0 commit comments

Comments
 (0)