Skip to content

Unclosed Unix socket causes ResourceWarning #3314

Closed
@kurtmckee

Description

Version: master branch, at d1b4191

Platform: Python 3.12 on Linux in CI

Description:

CI shows that there is an unclosed Unix socket when the test suite exits, which manifests as a ResourceWarning (recent example).

This is happening because redis-py isn't closing the Unix socket when the call to sock.connect() fails:

def _connect(self):
"Create a Unix domain socket connection"
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
sock.settimeout(self.socket_connect_timeout)
sock.connect(self.path)
sock.settimeout(self.socket_timeout)
return sock

The socket instance needs to be closed immediately if the call to sock.connect() fails, while the object can still be referenced.

You can recreate this situation with this trivial reproducer:

$ python -Walways -c 'import socket; socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)'
<string>:1: ResourceWarning: unclosed <socket.socket fd=3, family=1, type=1, proto=0>
ResourceWarning: Enable tracemalloc to get the object allocation traceback

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