From 78691a7ec589ef7109c4d98cd5f95a90cb0cd00e Mon Sep 17 00:00:00 2001 From: Alan Boudreault Date: Sun, 4 Jun 2017 11:19:31 -0400 Subject: [PATCH] Ensure asyncore dispatchers are closed on exit --- CHANGELOG.rst | 1 + cassandra/io/asyncorereactor.py | 10 +++++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 5423e039..b9afb171 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -4,6 +4,7 @@ Bug Fixes --------- * is_idempotent flag is not propagated from PreparedStatement to BoundStatement (PYTHON-736) +* Fix asyncore hang on exit (PYTHON-767) 3.10.0 ====== diff --git a/cassandra/io/asyncorereactor.py b/cassandra/io/asyncorereactor.py index 66fce9bb..04e9684e 100644 --- a/cassandra/io/asyncorereactor.py +++ b/cassandra/io/asyncorereactor.py @@ -240,6 +240,8 @@ class AsyncoreLoop(object): self._timers.add_timer(timer) def _cleanup(self): + global _dispatcher_map + self._shutdown = True if not self._thread: return @@ -253,6 +255,12 @@ class AsyncoreLoop(object): log.debug("Event loop thread was joined") + # Ensure all connections are closed and in-flight requests cancelled + for conn in tuple(_dispatcher_map.values()): + conn.close() + + log.debug("Dispatchers were closed") + class AsyncoreConnection(Connection, asyncore.dispatcher): """ @@ -326,7 +334,7 @@ class AsyncoreConnection(Connection, asyncore.dispatcher): #This happens when the connection is shutdown while waiting for the ReadyMessage if not self.connected_event.is_set(): self.last_error = ConnectionShutdown("Connection to %s was closed" % self.host) - + # don't leave in-progress operations hanging self.connected_event.set()