[3.11] gh-88863: Clear ref cycles to resolve leak when asyncio.open_connection raises (GH-95739) (#99721)

Break reference cycles to resolve memory leak, by
removing local exception and future instances from the frame.
(cherry picked from commit 995f6170c7)

Co-authored-by: Dong Uk, Kang <nailbrainz@gmail.com>
This commit is contained in:
Dong Uk, Kang 2022-11-24 03:37:24 +09:00 committed by GitHub
parent 609273eb52
commit 24fad64cef
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 36 additions and 12 deletions

View file

@ -630,7 +630,11 @@ class BaseSelectorEventLoop(base_events.BaseEventLoop):
fut = self.create_future()
self._sock_connect(fut, sock, address)
return await fut
try:
return await fut
finally:
# Needed to break cycles when an exception occurs.
fut = None
def _sock_connect(self, fut, sock, address):
fd = sock.fileno()
@ -652,6 +656,8 @@ class BaseSelectorEventLoop(base_events.BaseEventLoop):
fut.set_exception(exc)
else:
fut.set_result(None)
finally:
fut = None
def _sock_write_done(self, fd, fut, handle=None):
if handle is None or not handle.cancelled():
@ -675,6 +681,8 @@ class BaseSelectorEventLoop(base_events.BaseEventLoop):
fut.set_exception(exc)
else:
fut.set_result(None)
finally:
fut = None
async def sock_accept(self, sock):
"""Accept a connection.