Skip to content

Commit

Permalink
Close Unix sockets if the connection attempt fails (#3315)
Browse files Browse the repository at this point in the history
Make sure Unix sockets get closed if the connection fails.

Fixes #3314
  • Loading branch information
kurtmckee authored Jul 17, 2024
1 parent d1b4191 commit b2179f6
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
* Make `ClusterCommandsProtocol` an actual Protocol
* Add `sum` to DUPLICATE_POLICY documentation of `TS.CREATE`, `TS.ADD` and `TS.ALTER`
* Prevent async ClusterPipeline instances from becoming "false-y" in case of empty command stack (#3061)
* Close Unix sockets if the connection attempt fails. This prevents `ResourceWarning`s. (#3314)

* 4.1.3 (Feb 8, 2022)
* Fix flushdb and flushall (#1926)
Expand Down
7 changes: 6 additions & 1 deletion redis/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -920,7 +920,12 @@ 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)
try:
sock.connect(self.path)
except OSError:
# Prevent ResourceWarnings for unclosed sockets.
sock.close()
raise
sock.settimeout(self.socket_timeout)
return sock

Expand Down

0 comments on commit b2179f6

Please sign in to comment.