You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
asyncio: cancelling create_connection() with happy_eyeballs_delay leaks the connected socket #162059
When loop.create_connection() runs with happy_eyeballs_delay and is cancelled after an attempt has connected but before staggered_race() returns, the connected socket is never closed. It is only released by the garbage collector, with a ResourceWarning: unclosed <socket.socket ...>.
staggered_race() re-raises the CancelledError once its tasks finish, even when one of them has already won, so the winner's result (the socket) is dropped. create_connection() never sees it, so nothing closes it. Without happy_eyeballs_delay, _connect_sock() closes the socket on cancellation and nothing leaks.
Reproducer (a local server; cancel after 0–7 loop iterations):
A cancellation here is routine: asyncio.timeout() / wait_for() around open_connection(..., happy_eyeballs_delay=...), or a client closed while it connects.
Proposed fix: create_connection() remembers the sockets the attempts connect and closes them if the race raises. staggered_race() keeps propagating the cancellation as it does now.
This is not a real leak, the socket would be closed later anyways by the garbage collector. asyncio apps create a lot of cyclic garbage and it is not worth manually clearing cycles unless it holds on to large amount of memory.
Activity
kumaraditya303 commentedon Nov 17, 2026
This is not a real leak, the socket would be closed later anyways by the garbage collector. asyncio apps create a lot of cyclic garbage and it is not worth manually clearing cycles unless it holds on to large amount of memory.