From 5facc0e4f38e8f1e0bd9b8931e985a64a7e47dd6 Mon Sep 17 00:00:00 2001 From: Aymeric Augustin Date: Thu, 24 Jul 2025 08:20:34 +0200 Subject: [PATCH] Avoid leaking an open socket When connecting through a socks proxy to a non-existing or non-functional server such as `example.com` with the trio implementation, a warning is raised: ``` ResourceWarning: unclosed import gc; gc.collect() Object allocated at (most recent call last): File "python-3.13/lib/python3.13/site-packages/trio/_socket.py", lineno 381 stdlib_socket = _stdlib_socket.socket(family, type, proto, fileno) ``` This is the same issue with trio as 2e4f5ca9 was with asyncio. --- python_socks/async_/trio/_connect.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/python_socks/async_/trio/_connect.py b/python_socks/async_/trio/_connect.py index bc26b85..9419bbf 100644 --- a/python_socks/async_/trio/_connect.py +++ b/python_socks/async_/trio/_connect.py @@ -18,7 +18,11 @@ async def connect_tcp( if local_addr is not None: # pragma: no cover await sock.bind(local_addr) - await sock.connect((host, port)) + try: + await sock.connect((host, port)) + except OSError: + sock.close() + raise return sock