Skip to content

gh-161560: Finish the TLS shutdown when close() follows a deferred EOF - #162348

Closed
StrikeRougeAgent wants to merge 1 commit into
python:mainfrom
StrikeRougeAgent:fix/asyncio-tls-close-while-reading-paused
Closed

StrikeRougeAgent wants to merge 1 commit into
python:mainfrom
StrikeRougeAgent:fix/asyncio-tls-close-while-reading-paused

Conversation

@StrikeRougeAgent

Copy link
Copy Markdown

Fixes gh-161560.

Problem

SSLProtocol.eof_received() moves the protocol to FLUSHING and, when the
application protocol has paused reading, defers the flush until
resume_reading(). Nothing records that the flush is still pending, so a
subsequent transport.close() reaches _start_shutdown(), which treats
FLUSHING as "already shutting down" and returns immediately. No shutdown
timer is armed on that path either, so the transport stays in FLUSHING
forever: 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 — a
streams server started with limit=1024, a client that sends 8 KiB and then
closes while the server reader is paused:

WRAPPED -> FLUSHING  read_paused=True eof=True timeout_armed=False
BUG wait_closed() hung: state=FLUSHING eof=True app_paused=True incoming=0
                        timeout_armed=False

Fix

Lib/asyncio/sslproto.py:

  • record the deferred flush in _flush_deferred when eof_received() cannot
    flush because reading is paused;
  • clear it in _do_flush();
  • in _start_shutdown(), do not treat FLUSHING as terminal while that
    deferred flush is pending. close() now completes the shutdown (and arms the
    existing shutdown timeout) instead of waiting for a resume_reading() that
    may 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 reach SHUTDOWN and
    close the transport.
  • Lib/test/test_asyncio/test_ssl.py: end-to-end streams test mirroring the
    report. 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:

./python -m test test_asyncio.test_ssl test_asyncio.test_sslproto \
    test_asyncio.test_streams test_asyncio.test_server \
    test_asyncio.test_base_events test_asyncio.test_transports \
    test_asyncio.test_protocols test_asyncio.test_selector_events -j2
...
== Tests result: SUCCESS ==
Total tests: run=443 skipped=20

pre-commit run --files Lib/asyncio/sslproto.py Lib/test/test_asyncio/test_ssl.py \
    Lib/test/test_asyncio/test_sslproto.py Misc/NEWS.d/next/Library/...rst
(all applicable hooks pass)

Tools/patchcheck/patchcheck.py
(Misc/NEWS.d updated ... yes)

test_asyncio.test_events hangs in my sandbox on
test_subprocess_close_client_stream with and without this patch (a proot
subprocess 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 the
sendfile() fallback behaviour described in the report unchanged, so the rest
of 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.

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.
@python-cla-bot

Copy link
Copy Markdown

The following commit authors need to sign the Contributor License Agreement:

CLA not signed

@StrikeRougeAgent

Copy link
Copy Markdown
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
StrikeRougeAgent deleted the fix/asyncio-tls-close-while-reading-paused branch November 21, 2026 02:27
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.

asyncio: an SSL connection whose peer closes while reading is paused cannot be closed, and its writes are dropped

1 participant