Skip to content

Unclosed SSL sockets cause ResourceWarnings #3317

Closed
@kurtmckee

Description

@kurtmckee

Version: master branch, at d1b4191

Platform: Python 3.12 on Linux in CI

Description:

CI shows that the test suite opens SSL sockets but doesn't always close them, resulting in ResourceWarnings (recent example).

This is happening in two locations. In one location, a socket is opened but is only closed if an OSError is encountered...but a RedisError will be overlooked without also closing the socket:

sock = super()._connect()
try:
return self._wrap_socket_with_ssl(sock)
except OSError:
sock.close()
raise

In the second location, redis-py preemptively opens a second socket before it performs some validations that can result in a RedisError (specifically, the RedisError that is overlooked in the location shown above above). Once the exception is raised, there's no way to access and close the socket (sslsock in the code snippet below) that was opened:

sslsock = context.wrap_socket(sock, server_hostname=self.host)
if self.ssl_validate_ocsp is True and CRYPTOGRAPHY_AVAILABLE is False:
raise RedisError("cryptography is not installed.")
if self.ssl_validate_ocsp_stapled and self.ssl_validate_ocsp:
raise RedisError(
"Either an OCSP staple or pure OCSP connection must be validated "
"- not both."
)

These unclosed sockets eventually get called out as ResourceWarnings as the test suite exits.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions