From e59e0362fdb949edaa463fe2a9510f883b3e5163 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Sat, 14 Nov 2026 14:16:38 -0500 Subject: [PATCH 1/2] ssl: close the socket when a TLS handshake fails When the handshake in accept() or connect() failed, do_handshake() marked the SSLSocket closed but did not close the underlying socket. A later close() then did nothing, so the socket stayed open until it was garbage collected. For accept(), the caller never got the socket at all: a client that sent plain HTTP to a TLS server hung instead of being reset. Co-Authored-By: Claude Opus 5.5 --- shared-module/ssl/SSLSocket.c | 1 + 1 file changed, 1 insertion(+) diff --git a/shared-module/ssl/SSLSocket.c b/shared-module/ssl/SSLSocket.c index 5813c1a4d40..1a658069bd9 100644 --- a/shared-module/ssl/SSLSocket.c +++ b/shared-module/ssl/SSLSocket.c @@ -488,6 +488,7 @@ static void do_handshake(ssl_sslsocket_obj_t *self) { cleanup: self->closed = true; + ssl_socket_close(self); // Verification flags are only valid for CERT_VERIFY_FAILED. // Read them before mbedtls_ssl_free() below: they live in the ssl context's From 93a9175ef179916894a1d4e028c4c69a20bc3e40 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Sat, 14 Nov 2026 14:16:39 -0500 Subject: [PATCH 2/2] ssl: close the socket when wrap_socket() fails When mbedTLS setup failed, wrap_socket() freed the TLS state but left the new SSLSocket marked open. Its finaliser then closed the wrapped socket at some later garbage collection, while the caller might still be using it. Close it right away instead, as CPython does. Co-Authored-By: Claude Opus 5.5 --- shared-module/ssl/SSLSocket.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/shared-module/ssl/SSLSocket.c b/shared-module/ssl/SSLSocket.c index 1a658069bd9..4819412e07a 100644 --- a/shared-module/ssl/SSLSocket.c +++ b/shared-module/ssl/SSLSocket.c @@ -404,6 +404,8 @@ ssl_sslsocket_obj_t *common_hal_ssl_sslcontext_wrap_socket(ssl_sslcontext_obj_t } return o; cleanup: + o->closed = true; + ssl_socket_close(o); mbedtls_pk_free(&o->pkey); mbedtls_x509_crt_free(&o->cert); mbedtls_x509_crt_free(&o->cacert);