← 输入框|github.com|刷新
Skip to content

gh-160964: Keep asyncio SSL transport usable after close on lost connection - #160965

Open
asvetlov wants to merge 1 commit into
python:mainfrom
asvetlov:claude/cpython-pr-160069-status-c15db6
Open

asvetlov wants to merge 1 commit into
python:mainfrom
asvetlov:claude/cpython-pr-160069-status-c15db6

Conversation

@asvetlov

Copy link
Copy Markdown
Contributor

Closing an SSL transport that is already marked closed, for example from the protocol's connection_lost(), used to detach it from its SSLProtocol by setting _ssl_protocol = None (added in gh-98539). After that, write(), set_protocol(), get_write_buffer_size() and get_extra_info() raised AttributeError. The loop.sendfile() fallback hits this in restore() when the peer disconnects mid-transfer.

The transport now keeps its protocol. Instead, SSLProtocol._release_ssl() drops the SSL object, the context and their ssl_object/sslcontext extra info, which is what the detaching was meant to achieve. If the TLS shutdown is still in progress (for example when the peer started it, so the app's first close() already lands in that branch), the release waits until connection_lost().

A single close() behaves as before, so the app can still read get_extra_info('ssl_object') in its connection_lost().

Tests: two new unit tests in test_sslproto; both fail on main. The existing test_ssl.test_create_connection_memory_leak still passes, and test_asyncio and test_ssl pass on Linux.

🤖 Generated with Claude Code

…t connection

Closing an SSL transport that was already marked closed, e.g. from the
protocol's connection_lost(), detached it from its SSLProtocol, so later
calls such as write() or set_protocol() raised AttributeError.  This hit
the loop.sendfile() fallback when the peer went away mid-transfer.

Keep the protocol and instead release the SSL object and context, which
is what the detaching was for (pythonGH-98539).  If the shutdown is still in
progress, release them when the connection is lost.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@gpshead gpshead left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think this PR is correct, but it looks like you've already noticed the overlap with #160069 which also adds a sendfile and leak test. You might want to pick a few bits from that if those tests are relevant.

Logic difference: do we need to care about the release happening before or after theconnection_lost() callback? this one does it before, I think the other PR does it after.

@gpshead gpshead removed their assignment Oct 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants