Repository navigation
Deliver buffered TLS data before acting on a TCP EOF - #796
andrew-stelmach-fleet wants to merge 2 commits into
Conversation
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
left a comment
There was a problem hiding this comment.
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>
|
Thanks for the review. No flag should be needed: the change is confined to the platform-independent |
@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. |
|
@Vizonex thanks, I tried it on a winloop fork: the |
@andrew-stelmach-fleet |
|
@Vizonex sorry, I worded that badly: Windows has the same bug (the baseline run is winloop |
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 andconnection_lost(None)was reported; this happened with or without aclose_notifyin 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 withoutclose_notifyis still not reported to the application aseof_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), wheremastercloses 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
BufferedClientin #795 inheritsasyncio.Protocol, so uvloop treats it as non-buffered and the buffered read path is not exercised there),close_notifyfollowed by an immediate close, a truncated record, the server side, writing after the EOF, andclose()/abort()while the remaining data is being delivered. All of them fail onmaster.test_shutdown_timeout_handler_not_setis 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_renameon macOS, which also fails intermittently onmaster.AI disclosure: the patch and tests were written with Claude Code.
🤖 Generated with Claude Code