From d94bedfef3cf5e953f83ea2d69c6ae5d66234a8b Mon Sep 17 00:00:00 2001 From: Dana Powers Date: Thu, 8 Mar 2018 13:14:44 -0500 Subject: [PATCH] Short-circuit BrokerConnection.close() if already disconnected --- kafka/conn.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/kafka/conn.py b/kafka/conn.py index d778c3189..ed11c3c44 100644 --- a/kafka/conn.py +++ b/kafka/conn.py @@ -653,10 +653,13 @@ def close(self, error=None): will be failed with this exception. Default: kafka.errors.ConnectionError. """ + if self.state is ConnectionStates.DISCONNECTED: + if error is not None: + log.warning('%s: Duplicate close() with error: %s', self, error) + return log.info('%s: Closing connection. %s', self, error or '') - if self.state is not ConnectionStates.DISCONNECTED: - self.state = ConnectionStates.DISCONNECTING - self.config['state_change_callback'](self) + self.state = ConnectionStates.DISCONNECTING + self.config['state_change_callback'](self) self._update_reconnect_backoff() self._close_socket() self.state = ConnectionStates.DISCONNECTED