Skip to content

Commit b2179f6

Browse files
authored
Close Unix sockets if the connection attempt fails (#3315)
Make sure Unix sockets get closed if the connection fails. Fixes #3314
1 parent d1b4191 commit b2179f6

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

CHANGES

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
* Make `ClusterCommandsProtocol` an actual Protocol
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)
67+
* Close Unix sockets if the connection attempt fails. This prevents `ResourceWarning`s. (#3314)
6768

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

redis/connection.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -920,7 +920,12 @@ def _connect(self):
920920
"Create a Unix domain socket connection"
921921
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
922922
sock.settimeout(self.socket_connect_timeout)
923-
sock.connect(self.path)
923+
try:
924+
sock.connect(self.path)
925+
except OSError:
926+
# Prevent ResourceWarnings for unclosed sockets.
927+
sock.close()
928+
raise
924929
sock.settimeout(self.socket_timeout)
925930
return sock
926931

0 commit comments

Comments
 (0)