Repository navigation
gh-161560: Finish the TLS shutdown when close() follows a deferred EOF - #162348
Closed
StrikeRougeAgent wants to merge 1 commit into
Closed
StrikeRougeAgent wants to merge 1 commit into
StrikeRougeAgent wants to merge 1 commit into
Conversation
When the peer sent EOF while the application protocol had paused reading, eof_received() moved the SSL protocol to FLUSHING and deferred the flush until resume_reading(), but nothing recorded that the flush was still pending. Closing the transport in that window was a no-op: _start_shutdown() treated FLUSHING as "already shutting down", no shutdown timer was armed, and StreamWriter.wait_closed() never returned even though the peer was gone. Track the deferred flush and let close() complete it. The application will not read the buffered inbound data once it has closed the transport, so finishing the shutdown is better than parking the transport until a resume_reading() that may never come.
|
The following commit authors need to sign the Contributor License Agreement: |
Author
|
Closing this: the commit author is unable to sign the PSF CLA, so the change cannot be merged. The root-cause analysis, patch and tests in this description are left as-is in case someone else wants to pick it up. |
StrikeRougeAgent
deleted the
fix/asyncio-tls-close-while-reading-paused
branch
November 21, 2026 02:27
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes gh-161560.
Problem
SSLProtocol.eof_received()moves the protocol toFLUSHINGand, when theapplication protocol has paused reading, defers the flush until
resume_reading(). Nothing records that the flush is still pending, so asubsequent
transport.close()reaches_start_shutdown(), which treatsFLUSHINGas "already shutting down" and returns immediately. No shutdowntimer is armed on that path either, so the transport stays in
FLUSHINGforever: the buffered EOF is never processed and
StreamWriter.wait_closed()never returns, even though the peer is gone.
Reproduced on
main(ab5b8f87a7) with the scenario from the report — astreams server started with
limit=1024, a client that sends 8 KiB and thencloses while the server reader is paused:
Fix
Lib/asyncio/sslproto.py:_flush_deferredwheneof_received()cannotflush because reading is paused;
_do_flush();_start_shutdown(), do not treatFLUSHINGas terminal while thatdeferred flush is pending.
close()now completes the shutdown (and arms theexisting shutdown timeout) instead of waiting for a
resume_reading()thatmay never come.
The inbound data still buffered in the SSL protocol is not delivered in that
case; that matches
BaseTransport.close()("No more data will be received")because the application closed the transport while it had paused reading.
Tests
Lib/test/test_asyncio/test_sslproto.py: deterministic state-machine test —pause reading, deliver EOF, then
_start_shutdown()must reachSHUTDOWNandclose the transport.
Lib/test/test_asyncio/test_ssl.py: end-to-end streams test mirroring thereport. Without the fix the test hangs (30 s timeout); with it, it passes. Run
5× locally, no flakes.
Misc/NEWS.d/next/Library/2026-11-21-02-20-00.gh-issue-161560.Qv7RmT.rst.Validation
pydebug build of
main(ab5b8f87a7),./configure --with-pydebug:test_asyncio.test_eventshangs in my sandbox ontest_subprocess_close_client_streamwith and without this patch (a prootsubprocess limitation, not a regression), so it is not part of the run above.
Scope
This changes only the close path. It deliberately leaves
is_closing()and thesendfile()fallback behaviour described in the report unchanged, so the restof gh-161560 can be reviewed separately.
Disclosure: the root-cause analysis, patch and tests were prepared with an AI
coding agent (Hermes / StrikeRougeAgent) following CPython's AI-tools policy;
the diff, reproduction and test results above were reviewed before submission.