From 2323a0e4f1f386b361d51fea2ac1e1db815c6da1 Mon Sep 17 00:00:00 2001 From: Markus Gerstel Date: Fri, 21 Apr 2023 16:03:44 +0100 Subject: [PATCH] Fix potential race condition during disconnection When the disconnect() function is called twice in parallel it is possible that one thread deletes the self._sock reference, while the other thread will attempt to call .close() on it, leading to an AttributeError. This situation can routinely be encountered by closing the connection in a PubSubWorkerThread error handler in a blocking thread (ie. with sleep_time==None), and then calling .close() on the PubSub object. The main thread will then run into the disconnect() function, and the listener thread is woken up by the closure and will race into the disconnect() function, too. This can be fixed easily by copying the object reference before doing the None-check, similar to what we do in the redis.client.close() function. --- redis/connection.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/redis/connection.py b/redis/connection.py index faea7683f7..382bcddf4b 100644 --- a/redis/connection.py +++ b/redis/connection.py @@ -778,20 +778,22 @@ def on_connect(self): def disconnect(self, *args): "Disconnects from the Redis server" self._parser.on_disconnect() - if self._sock is None: + + socket = self._sock + self._sock = None + if socket is None: return if os.getpid() == self.pid: try: - self._sock.shutdown(socket.SHUT_RDWR) + socket.shutdown(socket.SHUT_RDWR) except OSError: pass try: - self._sock.close() + socket.close() except OSError: pass - self._sock = None def _send_ping(self): """Send PING, expect PONG in return"""