Fix SSL read over-consuming TCP data (#7418) · RustPython/RustPython@073adbd · GitHub
Skip to content

Commit 073adbd

Browse files
authored
Fix SSL read over-consuming TCP data (#7418)
Use single-record reading (recv_one_tls_record) for all SSL reads, not just handshake. This prevents rustls from eagerly consuming close_notify alongside application data, which left the TCP buffer empty and caused select()-based servers to miss readability and time out. Also fix recv_one_tls_record to return Eof (not WantRead) when peek returns empty bytes, since empty peek means the peer has closed the TCP connection.
1 parent 7c0981b commit 073adbd

3 files changed

Lines changed: 69 additions & 54 deletions

File tree

Lib/test/test_ftplib.py

Lines changed: 0 additions & 4 deletions

crates/stdlib/src/ssl.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3629,18 +3629,17 @@ mod _ssl {
36293629
return return_data(buf, &buffer, vm);
36303630
}
36313631
}
3632-
// Clean closure with close_notify
3633-
// CPython behavior depends on whether we've sent our close_notify:
3634-
// - If we've already sent close_notify (unwrap was called): raise SSLZeroReturnError
3635-
// - If we haven't sent close_notify yet: return empty bytes
3632+
// Clean closure via close_notify from peer.
3633+
// If we already sent close_notify (unwrap was called),
3634+
// raise SSLZeroReturnError (bidirectional shutdown).
3635+
// Otherwise return empty bytes, which callers (asyncore,
3636+
// asyncio sslproto) interpret as EOF.
36363637
let our_shutdown_state = *self.shutdown_state.lock();
36373638
if our_shutdown_state == ShutdownState::SentCloseNotify
36383639
|| our_shutdown_state == ShutdownState::Completed
36393640
{
3640-
// We already sent close_notify, now receiving peer's → SSLZeroReturnError
36413641
Err(create_ssl_zero_return_error(vm).upcast())
36423642
} else {
3643-
// We haven't sent close_notify yet → return empty bytes
36443643
return_data(vec![], &buffer, vm)
36453644
}
36463645
}

crates/stdlib/src/ssl/compat.rs

Lines changed: 64 additions & 44 deletions

0 commit comments

Comments
 (0)