Skip to content
Prev Previous commit
Next Next commit
Remove infinite loop "detection" from rustls glue
TLS handshake cannot be infinite. Any infinite loop here is a serious
bug in implementation and should be fixed properly.

This code triggers in some cases (very short reads) with misleading
`ssl_error.SSLWantReadError: The operation did not complete (read)`.
  • Loading branch information
im-0 committed May 21, 2026
commit 86ec36b3111c1fde7f022818db06b055f36303f9
14 changes: 3 additions & 11 deletions crates/stdlib/src/ssl/compat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1242,10 +1242,7 @@ pub(super) fn ssl_do_handshake(
let is_bio = socket.is_bio_mode();
let is_server = matches!(conn, Connection::Server(_));
let mut first_iteration = true; // Track if this is the first loop iteration
let mut iteration_count = 0;

loop {
iteration_count += 1;
let mut made_progress = false;

// IMPORTANT: In BIO mode, force initial write even if wants_write() is false
Expand Down Expand Up @@ -1363,11 +1360,6 @@ pub(super) fn ssl_do_handshake(
if !should_continue {
break;
}

// Safety check: prevent truly infinite loops (should never happen)
if iteration_count > 1000 {
break;
}
}

// If we exit the loop without completing handshake, return appropriate error
Expand All @@ -1381,9 +1373,9 @@ pub(super) fn ssl_do_handshake(
return Err(SslError::WantRead);
}
// Neither wants_read nor wants_write - this is a real error
Err(SslError::Syscall(format!(
"SSL handshake failed: incomplete after {iteration_count} iterations",
)))
Err(SslError::Syscall(
"SSL handshake failed: incomplete handshake".to_string(),
))
} else {
// Handshake completed successfully (shouldn't reach here normally)
Ok(())
Expand Down