diff --git a/valkey/connection.py b/valkey/connection.py index 0cb542c4..a4b6865c 100644 --- a/valkey/connection.py +++ b/valkey/connection.py @@ -815,7 +815,7 @@ def _connect(self): sock = super()._connect() try: return self._wrap_socket_with_ssl(sock) - except OSError: + except (OSError, ValkeyError): sock.close() raise @@ -850,7 +850,6 @@ def _wrap_socket_with_ssl(self, sock): context.minimum_version = self.ssl_min_version if self.ssl_ciphers: context.set_ciphers(self.ssl_ciphers) - sslsock = context.wrap_socket(sock, server_hostname=self.host) if self.ssl_validate_ocsp is True and CRYPTOGRAPHY_AVAILABLE is False: raise ValkeyError("cryptography is not installed.") @@ -860,6 +859,8 @@ def _wrap_socket_with_ssl(self, sock): "- not both." ) + sslsock = context.wrap_socket(sock, server_hostname=self.host) + # validation for the stapled case if self.ssl_validate_ocsp_stapled: import OpenSSL