@@ -1233,6 +1233,15 @@ fn handle_handshake_complete(
12331233 }
12341234 }
12351235
1236+ // CRITICAL: Ensure all pending TLS data is sent before returning
1237+ // TLS 1.3 Finished must reach server before handshake is considered complete
1238+ // Without this, server may not process application data
1239+ if !socket. is_bio_mode ( ) {
1240+ socket
1241+ . blocking_flush_all_pending ( vm)
1242+ . map_err ( SslError :: Py ) ?;
1243+ }
1244+
12361245 Ok ( true )
12371246}
12381247
@@ -1477,11 +1486,38 @@ pub(super) fn ssl_read(
14771486 // Check if connection needs to write data first (e.g., TLS key update, renegotiation)
14781487 // This mirrors the handshake logic which checks both wants_read() and wants_write()
14791488 if conn. wants_write ( ) && !is_bio {
1489+ // Check deadline BEFORE attempting flush
1490+ if let Some ( deadline) = deadline
1491+ && std:: time:: Instant :: now ( ) >= deadline
1492+ {
1493+ return Err ( SslError :: Timeout (
1494+ "The read operation timed out" . to_string ( ) ,
1495+ ) ) ;
1496+ }
1497+
14801498 // Flush pending TLS data before continuing
14811499 let tls_data = ssl_write_tls_records ( conn) ?;
14821500 if !tls_data. is_empty ( ) {
1483- send_all_bytes ( socket, tls_data, vm) ?;
1501+ // Use best-effort send - don't fail READ just because WRITE couldn't complete
1502+ match send_all_bytes ( socket, tls_data, vm) {
1503+ Ok ( ( ) ) => { }
1504+ Err ( SslError :: WantWrite ) => {
1505+ // Socket buffer full - acceptable during READ operation
1506+ // Pending data will be sent on next write/read call
1507+ }
1508+ Err ( e) => return Err ( e) ,
1509+ }
14841510 }
1511+
1512+ // Check deadline AFTER flush attempt
1513+ if let Some ( deadline) = deadline
1514+ && std:: time:: Instant :: now ( ) >= deadline
1515+ {
1516+ return Err ( SslError :: Timeout (
1517+ "The read operation timed out" . to_string ( ) ,
1518+ ) ) ;
1519+ }
1520+
14851521 // After flushing, rustls may want to read again - continue loop
14861522 continue ;
14871523 }
0 commit comments