Skip to content

Commit 1634946

Browse files
committed
Handle possible errors at SSL handshake
1 parent 404405a commit 1634946

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

redis/connection.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -834,8 +834,26 @@ def __init__(
834834
super().__init__(**kwargs)
835835

836836
def _connect(self):
837-
"Wrap the socket with SSL support"
837+
"""
838+
Wrap the socket with SSL support, handling potential errors.
839+
"""
838840
sock = super()._connect()
841+
try:
842+
return self._wrap_socket_with_ssl(sock)
843+
except OSError:
844+
sock.close()
845+
raise
846+
847+
def _wrap_socket_with_ssl(self, sock):
848+
"""
849+
Wraps the socket with SSL support.
850+
851+
Args:
852+
sock: The plain socket to wrap with SSL.
853+
854+
Returns:
855+
An SSL wrapped socket.
856+
"""
839857
context = ssl.create_default_context()
840858
context.check_hostname = self.check_hostname
841859
context.verify_mode = self.cert_reqs

0 commit comments

Comments
 (0)