Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix RecursionError because of repeated channel reconnections. #380

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions amqp/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
consumer_tag=%r exchange=%r routing_key=%r.\
"""

MAX_RECONNECTIONS = 128


class VDeprecationWarning(DeprecationWarning):
pass
Expand Down Expand Up @@ -116,6 +118,8 @@ def __init__(self, connection,

self.on_open = ensure_promise(on_open)

self.reconnection_count = 0

# set first time basic_publish_confirm is called
# and publisher confirms are enabled for this channel.
self._confirm_selected = False
Expand Down Expand Up @@ -162,6 +166,7 @@ def collect(self):

def _do_revive(self):
self.is_open = False
self.reconnection_count += 1
self.open()

def close(self, reply_code=0, reply_text='', method_sig=(0, 0),
Expand Down Expand Up @@ -277,6 +282,10 @@ def _on_close(self, reply_code, reply_text, class_id, method_id):
"""
self.send_method(spec.Channel.CloseOk)
if not self.connection.is_closing:
if self.reconnection_count >= MAX_RECONNECTIONS:
raise error_for_code(
reply_code, reply_text, (class_id, method_id), ChannelError,
)
self._do_revive()
raise error_for_code(
reply_code, reply_text, (class_id, method_id), ChannelError,
Expand Down Expand Up @@ -445,6 +454,7 @@ def _on_open_ok(self):
"""
self.is_open = True
self.on_open(self)
self.reconnection_count = 0
AMQP_LOGGER.debug('Channel open')

#############
Expand Down
12 changes: 12 additions & 0 deletions t/unit/test_channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,18 @@ def test_close(self):
assert self.c.is_closing is False
assert self.c.connection is None

def test_on_close_exceeds_max_reconnections(self):
assert self.c.reconnection_count == 0

def mocked_on_revive():
self.c.reconnection_count += 1
self.c._on_close(404, 'text', 50, 61)
self.c._do_revive = Mock(name='_do_revive', side_effect=mocked_on_revive)
with pytest.raises(NotFound):
self.c._on_close(404, 'text', 50, 61)
from amqp.channel import MAX_RECONNECTIONS
assert self.c.reconnection_count == MAX_RECONNECTIONS

def test_on_close(self):
self.c._do_revive = Mock(name='_do_revive')
with pytest.raises(NotFound):
Expand Down