diff --git a/python_apps/airtime_analyzer/airtime_analyzer/message_listener.py b/python_apps/airtime_analyzer/airtime_analyzer/message_listener.py index 97613e81f..9b890321c 100644 --- a/python_apps/airtime_analyzer/airtime_analyzer/message_listener.py +++ b/python_apps/airtime_analyzer/airtime_analyzer/message_listener.py @@ -120,8 +120,13 @@ class MessageListener: def disconnect_from_messaging_server(self): '''Stop consuming RabbitMQ messages and disconnect''' - self._channel.stop_consuming() - self._connection.close() + # If you try to close a connection that's already closed, you're going to have a bad time. + # We're breaking EAFP because this can be called multiple times depending on exception + # handling flow here. + if not self._channel.is_closed and not self._channel.is_closing: + self._channel.stop_consuming() + if not self._connection.is_closed and not self._connection.is_closing: + self._connection.close() def graceful_shutdown(self, signum, frame): '''Disconnect and break out of the message listening loop'''