Skip to content

Deliver buffered TLS data before acting on a TCP EOF - #796

Open
andrew-stelmach-fleet wants to merge 2 commits into
MagicStack:masterfrom
andrew-stelmach-fleet:fix-ssl-eof-buffered-data
Open

andrew-stelmach-fleet wants to merge 2 commits into
MagicStack:masterfrom
andrew-stelmach-fleet:fix-ssl-eof-buffered-data

Conversation

@andrew-stelmach-fleet

@andrew-stelmach-fleet andrew-stelmach-fleet commented Nov 30, 2026 •

Copy link
Copy Markdown

Fixes #792. This is an alternative to #795.

On a TCP EOF, SSLProtocol.eof_received() closed the transport right away, so plaintext that had not been delivered yet (e.g. because the application paused reading) was dropped and connection_lost(None) was reported; this happened with or without a close_notify in front of the EOF. The EOF is now only recorded, and the regular read path reaches the end of the incoming data in order and starts the shutdown from there, so no pending-byte bookkeeping is needed. A TCP EOF without close_notify is still not reported to the application as eof_received(). One behaviour change to be aware of: if the application has paused reading and nothing is buffered, the connection now stays open until it resumes reading or closes (as in asyncio), where master closes it at once; #795 keeps the immediate close under TLS 1.2 but not under TLS 1.3, where unread session tickets count as pending data.

The tests cover both protocol kinds (the BufferedClient in #795 inherits asyncio.Protocol, so uvloop treats it as non-buffered and the buffered read path is not exercised there), close_notify followed by an immediate close, a truncated record, the server side, writing after the EOF, and close()/abort() while the remaining data is being delivered. All of them fail on master. test_shutdown_timeout_handler_not_set is updated because it relied on a bare EOF closing a paused connection and dropping its data.

Full suite run locally on macOS (3.9–3.15 and 3.14t, system libuv 1.53.0) and Linux (3.9, 3.12, 3.14, 3.15, 3.14t, 3.15t, vendored libuv, arm64 and x86_64); the only failure is test_fs_event_rename on macOS, which also fails intermittently on master.

AI disclosure: the patch and tests were written with Claude Code.

🤖 Generated with Claude Code

When the peer closed the TCP connection while received data had not been
delivered to the application yet - e.g. because reading was paused -
SSLProtocol.eof_received() closed the transport right away and the data
was lost, with connection_lost(None) reported. This happened both with
and without a close_notify in front of the EOF.

The EOF is the last thing in the incoming stream, so only record it in
eof_received() and let the regular read path run into the end of the
incoming data in order: after the remaining data, SSLObject.read()
reports either the close_notify, or that it wants more data than will
ever come, and the shutdown starts from there. The transport is kept
open until then. A TCP EOF without close_notify is still not reported
to the application as eof_received().

Fixes MagicStack#792.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

@Vizonex Vizonex left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I would recommend adding a flag for sslproto if you think this is just linux or mac related because windows support is coming and I'm trying to prepare everybody for when that finally occurs. Hope that makes sense as to why I've been in the process of reviewing so many pull requests recently.

os.dup() does not work on socket handles on Windows.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@andrew-stelmach-fleet

andrew-stelmach-fleet commented Nov 30, 2026 •

Copy link
Copy Markdown
Author

Thanks for the review. No flag should be needed: the change is confined to the platform-independent SSLProtocol state machine (not yet run on Windows), and I switched the one test that used os.dup() on a socket to socket.fromfd() so it works there too.

@Vizonex

Vizonex commented Nov 30, 2026

Copy link
Copy Markdown

Thanks for taking a look. I don't think a flag is needed: nothing here is Linux- or macOS-specific. The change is confined to the SSLProtocol state machine, which works on memory BIOs, and the EOF reaches it through UVStream from libuv's UV_EOF, so the same path should apply on Windows. I have not run it on Windows, though.

One test used os.dup() on a socket, which would not work on Windows; I switched it to socket.fromfd() in a follow-up commit.

Since #795 is already approved: both PRs fix #792, and #795 passes the tests added here. I updated the description with the one behaviour difference I found (a paused reader with nothing buffered at EOF). If you'd rather go with #795, the tests here could still be useful on top of it, since its BufferedClient inherits asyncio.Protocol and so doesn't reach the buffered read path.

@andrew-stelmach-fleet I'll let you test it on https://github.com/Vizonex/winloop if you want I have no problems with that, Just know that some of it is modified vs uvloop as I have been looking for ways to optimize uvloop as a whole.

andrew-stelmach-fleet added a commit to andrew-stelmach-fleet/Winloop that referenced this pull request Nov 30, 2026
@andrew-stelmach-fleet

Copy link
Copy Markdown
Author

@Vizonex thanks, I tried it on a winloop fork: the sslproto patch applies to winloop main unchanged, and with it the new tests and winloop's full suite pass on Windows x64 (3.10, 3.12, 3.14, 3.14t, 3.15) and Windows ARM (3.13) — run. Without the patch the new tests fail there too (baseline run).

@Vizonex

Vizonex commented Nov 30, 2026

Copy link
Copy Markdown

@Vizonex thanks, I tried it on a winloop fork: the sslproto patch applies to winloop main unchanged, and with it the new tests and winloop's full suite pass on Windows x64 (3.10, 3.12, 3.14, 3.14t, 3.15) and Windows ARM (3.13) — run. Without the patch the new tests fail there too (baseline run).

@andrew-stelmach-fleet
Based on that I would add flags that make it so that these features are not reached by using the system.PLATFORM_IS flags to filter out windows and have it fallback to what it was originally that way when these changes are added windows is unaffected by the change.

@andrew-stelmach-fleet

Copy link
Copy Markdown
Author

@Vizonex sorry, I worded that badly: Windows has the same bug (the baseline run is winloop main plus only the new tests, and they fail because buffered data is dropped), and with the patch everything passes there, full suite included. So gating Windows out would keep the data loss on Windows only; I'd rather leave it ungated unless you've seen a Windows-specific problem with it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SSL transport drops buffered data when peer closes TCP without close_notify while reading is paused

2 participants