Skip to content

Commit 9607608

Browse files
authored
Close SSL sockets when connections/validations fail (#3318)
Handle more cases of failure when initializing SSL sockets, and make sure no socket is left unclosed in case of errors. Fixes #3317
1 parent b206a0f commit 9607608

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

CHANGES

+1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
* Add `sum` to DUPLICATE_POLICY documentation of `TS.CREATE`, `TS.ADD` and `TS.ALTER`
6666
* Prevent async ClusterPipeline instances from becoming "false-y" in case of empty command stack (#3061)
6767
* Close Unix sockets if the connection attempt fails. This prevents `ResourceWarning`s. (#3314)
68+
* Close SSL sockets if the connection attempt fails, or if validations fail. (#3317)
6869

6970
* 4.1.3 (Feb 8, 2022)
7071
* Fix flushdb and flushall (#1926)

redis/connection.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -819,7 +819,7 @@ def _connect(self):
819819
sock = super()._connect()
820820
try:
821821
return self._wrap_socket_with_ssl(sock)
822-
except OSError:
822+
except (OSError, RedisError):
823823
sock.close()
824824
raise
825825

@@ -854,7 +854,6 @@ def _wrap_socket_with_ssl(self, sock):
854854
context.minimum_version = self.ssl_min_version
855855
if self.ssl_ciphers:
856856
context.set_ciphers(self.ssl_ciphers)
857-
sslsock = context.wrap_socket(sock, server_hostname=self.host)
858857
if self.ssl_validate_ocsp is True and CRYPTOGRAPHY_AVAILABLE is False:
859858
raise RedisError("cryptography is not installed.")
860859

@@ -864,6 +863,8 @@ def _wrap_socket_with_ssl(self, sock):
864863
"- not both."
865864
)
866865

866+
sslsock = context.wrap_socket(sock, server_hostname=self.host)
867+
867868
# validation for the stapled case
868869
if self.ssl_validate_ocsp_stapled:
869870
import OpenSSL

0 commit comments

Comments
 (0)