]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.12] gh-124858: fix happy eyeballs refcyles (GH-124859) (#124913)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Wed, 23 Oct 2024 19:34:11 +0000 (21:34 +0200)
committerGitHub <noreply@github.com>
Wed, 23 Oct 2024 19:34:11 +0000 (12:34 -0700)
gh-124858: fix happy eyeballs refcyles (GH-124859)
(cherry picked from commit c066bf553577d1000e208eb078d9e758c3e41186)

Co-authored-by: Thomas Grainger <tagrain@gmail.com>
Lib/asyncio/base_events.py
Lib/asyncio/staggered.py
Lib/test/test_asyncio/test_streams.py
Misc/NEWS.d/next/Library/2024-10-01-17-12-20.gh-issue-124858.Zy0tvT.rst [new file with mode: 0644]

index cb037fd472c5aa3bed4b64cfa401cc9889768402..3146f7f3f655035e16e791ca2590994d38ca4c76 100644 (file)
@@ -17,7 +17,6 @@ import collections
 import collections.abc
 import concurrent.futures
 import errno
-import functools
 import heapq
 import itertools
 import os
@@ -1106,11 +1105,18 @@ class BaseEventLoop(events.AbstractEventLoop):
                     except OSError:
                         continue
             else:  # using happy eyeballs
-                sock, _, _ = await staggered.staggered_race(
-                    (functools.partial(self._connect_sock,
-                                       exceptions, addrinfo, laddr_infos)
-                     for addrinfo in infos),
-                    happy_eyeballs_delay, loop=self)
+                sock = (await staggered.staggered_race(
+                    (
+                        # can't use functools.partial as it keeps a reference
+                        # to exceptions
+                        lambda addrinfo=addrinfo: self._connect_sock(
+                            exceptions, addrinfo, laddr_infos
+                        )
+                        for addrinfo in infos
+                    ),
+                    happy_eyeballs_delay,
+                    loop=self,
+                ))[0]  # can't use sock, _, _ as it keeks a reference to exceptions
 
             if sock is None:
                 exceptions = [exc for sub in exceptions for exc in sub]
index 7aafcea4d885eb5819f7b9efdb7f00bb8e984487..0f4df8855a80b91b13c85054e3c8f713b2b690f0 100644 (file)
@@ -144,6 +144,7 @@ async def staggered_race(coro_fns, delay, *, loop=None):
                         raise d.exception()
         return winner_result, winner_index, exceptions
     finally:
+        del exceptions
         # Make sure no tasks are left running if we leave this function
         for t in running_tasks:
             t.cancel()
index 3c8cc5f3649180d15ddb1a5a00f26008f25d8351..686fef8377f4e5fb8aed57ba2074f796f1912eef 100644 (file)
@@ -1166,6 +1166,24 @@ os.close(fd)
         messages = self._basetest_unhandled_exceptions(handle_echo)
         self.assertEqual(messages, [])
 
+    def test_open_connection_happy_eyeball_refcycles(self):
+        port = socket_helper.find_unused_port()
+        async def main():
+            exc = None
+            try:
+                await asyncio.open_connection(
+                    host="localhost",
+                    port=port,
+                    happy_eyeballs_delay=0.25,
+                )
+            except* OSError as excs:
+                # can't use assertRaises because that clears frames
+                exc = excs.exceptions[0]
+            self.assertIsNotNone(exc)
+            self.assertListEqual(gc.get_referrers(exc), [])
+
+        asyncio.run(main())
+
 
 if __name__ == '__main__':
     unittest.main()
diff --git a/Misc/NEWS.d/next/Library/2024-10-01-17-12-20.gh-issue-124858.Zy0tvT.rst b/Misc/NEWS.d/next/Library/2024-10-01-17-12-20.gh-issue-124858.Zy0tvT.rst
new file mode 100644 (file)
index 0000000..c05d24a
--- /dev/null
@@ -0,0 +1 @@
+Fix reference cycles left in tracebacks in :func:`asyncio.open_connection` when used with ``happy_eyeballs_delay``