Skip to content

gh-161560: Fix asyncio TLS transports stuck after EOF while reading is paused - #163175

Open
dzakat wants to merge 2 commits into
python:mainfrom
dzakat:gh-161560-ssl-flushing-close
Open

dzakat wants to merge 2 commits into
python:mainfrom
dzakat:gh-161560-ssl-flushing-close

Conversation

@dzakat

@dzakat dzakat commented Dec 4, 2026 •

Copy link
Copy Markdown

When the peer sends EOF while the application has paused reading, SSLProtocol stays in the FLUSHING state until resume_reading() is called, so the application can still read the data that arrived before the EOF. While it waits there, writes are silently dropped, but is_closing() returns False and close() returns early. So loop.sendfile() keeps "sending" and reports the whole file as sent, and if the application never resumes reading, the connection is never closed.

The fix follows the approach cornopaez suggested in the issue:

  • is_closing() returns True in FLUSHING and SHUTDOWN, the two states where writes are dropped. With this, the sendfile() fallback raises ConnectionError instead of reporting data as sent.
  • close() in the waiting state finishes the flush and shutdown. Data the application never read is discarded, the same as close() with reading paused in any other state.
  • _do_flush() checks the state again after data_received() returns. That callback can now close or abort the transport in the middle of a flush, which used to raise RuntimeError from _set_state(). The abort case already failed this way on main.

If the application resumes reading instead of closing, nothing changes: it still gets the data sent before the EOF.

This builds on the analysis in #162348, which was closed because its author could not sign the CLA. The main difference is that this PR also fixes is_closing() and sendfile(), and it uses the existing state instead of adding a new attribute.

Tests: two functional tests in test_ssl (close() after EOF while paused, and sendfile() after EOF) that fail on main, plus unit tests in test_sslproto for closing or aborting from inside data_received() during a flush, and for resume_reading() after EOF still delivering the buffered data while is_closing() is true. test_asyncio and test_ssl pass on a macOS debug build.

…ding is paused

When the peer sent EOF while the application had paused reading, the
SSL protocol waited in the FLUSHING state for resume_reading(), so that
the application could still read what the peer sent before the EOF.
In that state writes were silently dropped, but is_closing() returned
False and close() returned early, so loop.sendfile() kept "sending" and
reported the whole file as sent, and the connection was never closed if
the application did not resume reading.

is_closing() now returns True in the FLUSHING and SHUTDOWN states, and
close() completes the shutdown from FLUSHING when reading is paused.
The flush also checks the state after calling data_received(), which
may now close the transport, or abort it, before the flush finishes.
@python-cla-bot

python-cla-bot Bot commented Dec 4, 2026 •

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

@efanntyo

efanntyo commented Dec 5, 2026

Copy link
Copy Markdown

Could the non-closing resume path be retained as an explicit control alongside the new close/abort cases? It protects the distinction in the description: is_closing() is now true in FLUSHING, while resume_reading() must still deliver pre-EOF application data.

I exercised that path on Python 3.12.16/Linux with a mocked SSL read sequence [b'buffered-before-fin', SSLWantReadError], state FLUSHING, application reading paused, _eof_received=True, and a transport whose is_closing() returns False. After resume_reading() and draining the loop, the installed control and the three exact PR methods from e97a593 both delivered the chunk once, called application EOF once, reached SHUTDOWN and scheduled transport close once. The expected difference was the initial application is_closing(): False on the control, True with the PR methods.

A sibling test without close/abort in data_received() could assert both the changed status and preserved delivery. This is only a mocked-BIO/state-machine control with _is_transport_closing, _start_shutdown and _do_flush substituted; it is not a real TLS handshake, sendfile test, full PR build or test-suite result. AI-assisted analysis; the control was executed.

is_closing() is now true while the protocol waits in FLUSHING, but
resume_reading() must still pass what the peer sent before the EOF to
the application, then complete the shutdown.
@dzakat

dzakat commented Dec 5, 2026

Copy link
Copy Markdown
Author

@efanntyo good point, thanks. Added test_resume_reading_after_eof: after EOF while paused, is_closing() is True, then resume_reading() still delivers the buffered data once, calls eof_received() once and closes the transport. Against main it fails only on the is_closing() assertion.

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