From ca27658c853f1004346024bbd6a8c06d5eba80b3 Mon Sep 17 00:00:00 2001 From: Dmitry Kropachev Date: Sat, 14 Feb 2026 07:59:51 -0400 Subject: [PATCH] Fix TwistedConnection close() not setting last_error (#614) When close() is called during connection setup (before connected_event is set), factory() could return a dead connection because last_error was not set. Copy the pattern from asyncore where last_error is set in close() when the connection handshake hasn't completed yet. --- cassandra/io/twistedreactor.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/cassandra/io/twistedreactor.py b/cassandra/io/twistedreactor.py index 446200bf63..26d046f3ba 100644 --- a/cassandra/io/twistedreactor.py +++ b/cassandra/io/twistedreactor.py @@ -286,7 +286,10 @@ def close(self): msg = "Connection to %s was closed" % self.endpoint if self.last_error: msg += ": %s" % (self.last_error,) - self.error_all_requests(ConnectionShutdown(msg)) + shutdown_exc = ConnectionShutdown(msg) + self.error_all_requests(shutdown_exc) + if not self.connected_event.is_set(): + self.last_error = shutdown_exc # don't leave in-progress operations hanging self.connected_event.set()