Repository navigation
Conversation
…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.
|
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: I exercised that path on Python 3.12.16/Linux with a mocked SSL read sequence A sibling test without close/abort in |
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.
|
@efanntyo good point, thanks. Added |
When the peer sends EOF while the application has paused reading,
SSLProtocolstays in theFLUSHINGstate untilresume_reading()is called, so the application can still read the data that arrived before the EOF. While it waits there, writes are silently dropped, butis_closing()returnsFalseandclose()returns early. Soloop.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()returnsTrueinFLUSHINGandSHUTDOWN, the two states where writes are dropped. With this, thesendfile()fallback raisesConnectionErrorinstead of reporting data as sent.close()in the waiting state finishes the flush and shutdown. Data the application never read is discarded, the same asclose()with reading paused in any other state._do_flush()checks the state again afterdata_received()returns. That callback can now close or abort the transport in the middle of a flush, which used to raiseRuntimeErrorfrom_set_state(). The abort case already failed this way onmain.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()andsendfile(), and it uses the existing state instead of adding a new attribute.Tests: two functional tests in
test_ssl(close()after EOF while paused, andsendfile()after EOF) that fail onmain, plus unit tests intest_sslprotofor closing or aborting from insidedata_received()during a flush, and forresume_reading()after EOF still delivering the buffered data whileis_closing()is true.test_asyncioandtest_sslpass on a macOS debug build.