From 8a7ea091bfcce802f136b3bfef28a562a3962a81 Mon Sep 17 00:00:00 2001 From: Ask Solem Date: Wed, 7 Dec 2016 14:28:16 -0800 Subject: [PATCH] Consumer: __exit__ should not cancel if connection error (Closes #670) --- kombu/messaging.py | 13 ++++++++----- t/unit/test_messaging.py | 9 +++++++++ 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/kombu/messaging.py b/kombu/messaging.py index 135da6d0..5fbccdb5 100644 --- a/kombu/messaging.py +++ b/kombu/messaging.py @@ -430,11 +430,14 @@ class Consumer(object): self.consume() return self - def __exit__(self, *exc_info): - try: - self.cancel() - except Exception: - pass + def __exit__(self, exc_type, exc_val, exc_tb): + if self.channel: + conn_errors = self.channel.connection.client.connection_errors + if not isinstance(exc_val, conn_errors): + try: + self.cancel() + except Exception: + pass def add_queue(self, queue): """Add a queue to the list of queues to consume from. diff --git a/t/unit/test_messaging.py b/t/unit/test_messaging.py index b0a5876f..7ca790c9 100644 --- a/t/unit/test_messaging.py +++ b/t/unit/test_messaging.py @@ -262,6 +262,15 @@ class test_Consumer: pass c.cancel.assert_called_with() + def test_enter_exit_cancel_not_called_on_connection_error(self): + c = Consumer(self.connection) + c.cancel = Mock(name='Consumer.cancel') + assert self.connection.connection_errors + with pytest.raises(self.connection.connection_errors[0]): + with c: + raise self.connection.connection_errors[0]() + c.cancel.assert_not_called() + def test_receive_callback_accept(self): message = Mock(name='Message') message.errors = []