Skip to content

gh-159049: Fix reference cycles after a failed TLS handshake in asyncio - #159140

Merged
kumaraditya303 merged 7 commits into
python:mainfrom
deadlovelll:gh-159049-refcycle
Oct 18, 2026
Merged

kumaraditya303 merged 7 commits into
python:mainfrom
deadlovelll:gh-159049-refcycle

Conversation

@deadlovelll

Copy link
Copy Markdown
Contributor

Fix reference cycles after a failed TLS handshake in asyncio

For details see gh-159049

Comment thread Lib/asyncio/base_events.py Outdated
await waiter
except:
transport.close()
# gh-159049

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the comment is not useful here

Comment thread Lib/asyncio/selector_events.py Outdated
super()._call_connection_lost(exc)
finally:
self._write_ready = None
# gh-159049: clear callback too

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

def server(sock):
try:
sock.start_tls(sslctx, server_side=True)
except OSError:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this try-except really needed?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, handshake always fails here, so without except server will crash the test

Comment thread Lib/test/test_asyncio/test_sslproto.py Outdated
support.gc_collect()
with self.tcp_server(server) as srv, support.disable_gc():
self.loop.run_until_complete(conn(srv.addr))
self.assertFalse([o for o in gc.get_objects()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using gc. get_objects is fragile, a better way to write this test is to have weakrefs for the transport and check that it is none at this point.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've addressed all the comments, could you please take another look?

@kumaraditya303 kumaraditya303 added topic-asyncio needs backport to 3.14 bugs and security fixes needs backport to 3.15 bugs and security fixes labels Oct 18, 2026
@kumaraditya303

Copy link
Copy Markdown
Contributor

I think there is a similar ref cycle present in BaseEventLoop.start_tls that needs a similar fix.

@deadlovelll

Copy link
Copy Markdown
Contributor Author

I think there is a similar ref cycle present in BaseEventLoop.start_tls that needs a similar fix.

I'll check it in a couple of hours. If yes, is that okay if I push the fix into this PR without new issue+pr?

@kumaraditya303

Copy link
Copy Markdown
Contributor

Yes, push directly to this PR.

@deadlovelll

deadlovelll commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor Author

Yes, push directly to this PR.

Yes, the cycle has been confirmed. I’ve pushed the changes

@kumaraditya303
kumaraditya303 merged commit 7dbc797 into python:main Oct 18, 2026
57 checks passed
@miss-islington-app

Copy link
Copy Markdown

Thanks @deadlovelll for the PR, and @kumaraditya303 for merging it 🌮🎉.. I'm working now to backport this PR to: 3.14, 3.15.
🐍🍒⛏🤖

@miss-islington-app

Copy link
Copy Markdown

Sorry, @deadlovelll and @kumaraditya303, I could not cleanly backport this to 3.15 due to a conflict.

Please backport manually with cherry_picker, see the devguide for more information.

cherry_picker 7dbc7975db8e34db8be8e4a10bd10784ab49b07a 3.15

@miss-islington-app

Copy link
Copy Markdown

Sorry, @deadlovelll and @kumaraditya303, I could not cleanly backport this to 3.14 due to a conflict.

Please backport manually with cherry_picker, see the devguide for more information.

cherry_picker 7dbc7975db8e34db8be8e4a10bd10784ab49b07a 3.14

@deadlovelll

Copy link
Copy Markdown
Contributor Author

i'll work on the bp

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs backport to 3.14 bugs and security fixes needs backport to 3.15 bugs and security fixes topic-asyncio

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants