question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Stack overflow using Asynchronous Consumer Example

See original GitHub issue

I’m testing the pika/examples/asynchronous_consumer_example.py and deliberately have my RabbitMQ server offline to see how it handles the connection-errors.

I updated the self._connection.ioloop.call_later(5, self.reconnect) in the on_connection_open_error method from 5 seconds to 0 seconds, because in a previous version of this async consumer I found an issue that after many attempts, I got a recursion error (https://github.com/pika/pika/issues/399) and the following PR (https://github.com/pika/pika/pull/710) was only for producers, not consumers.

Creating the consumer and calling run(), directly fails (as predicted) that it cannot connect to the broker (which on purpose does not exist), but after a very short while (approx 1 sec) it makes an ugly crash with the following:

Fatal Python error: Cannot recover from stack overflow.

Current thread 0x00007fa412e1a700 (most recent call first):
  File "/usr/local/lib/python3.6/site-packages/pika-1.0.0b1-py3.6.egg/pika/adapters/base_connection.py", line 113 in __repr__
  File "/usr/local/lib/python3.6/logging/__init__.py", line 338 in getMessage
  File "/usr/local/lib/python3.6/logging/__init__.py", line 575 in format
  File "/usr/local/lib/python3.6/logging/__init__.py", line 838 in format
  File "/usr/local/lib/python3.6/logging/__init__.py", line 992 in emit
  File "/usr/local/lib/python3.6/logging/__init__.py", line 863 in handle
  File "/usr/local/lib/python3.6/logging/__init__.py", line 1514 in callHandlers
  File "/usr/local/lib/python3.6/logging/__init__.py", line 1452 in handle
  File "/usr/local/lib/python3.6/logging/__init__.py", line 1442 in _log
  File "/usr/local/lib/python3.6/logging/__init__.py", line 1306 in info
  File "/usr/src/app/consumer/async_consumer.py", line 106 in on_connection_error
  File "/usr/local/lib/python3.6/site-packages/pika-1.0.0b1-py3.6.egg/pika/callback.py", line 236 in process
  File "/usr/local/lib/python3.6/site-packages/pika-1.0.0b1-py3.6.egg/pika/callback.py", line 92 in wrapper
  File "/usr/local/lib/python3.6/site-packages/pika-1.0.0b1-py3.6.egg/pika/callback.py", line 60 in wrapper
  File "/usr/local/lib/python3.6/site-packages/pika-1.0.0b1-py3.6.egg/pika/connection.py", line 2172 in _on_stream_terminated
  File "/usr/local/lib/python3.6/site-packages/pika-1.0.0b1-py3.6.egg/pika/adapters/base_connection.py", line 375 in _handle_connection_workflow_failure
  File "/usr/local/lib/python3.6/site-packages/pika-1.0.0b1-py3.6.egg/pika/adapters/base_connection.py", line 352 in _on_connection_workflow_done
  File "/usr/local/lib/python3.6/site-packages/pika-1.0.0b1-py3.6.egg/pika/adapters/base_connection.py", line 276 in _unshim_connection_workflow_callback
  File "/usr/local/lib/python3.6/site-packages/pika-1.0.0b1-py3.6.egg/pika/adapters/utils/connection_workflow.py", line 753 in _report_completion_and_cleanup
  File "/usr/local/lib/python3.6/site-packages/pika-1.0.0b1-py3.6.egg/pika/adapters/utils/connection_workflow.py", line 770 in _start_new_cycle_async
  File "/usr/local/lib/python3.6/site-packages/pika-1.0.0b1-py3.6.egg/pika/adapters/utils/connection_workflow.py", line 824 in _on_getaddrinfo_async_done
  File "/usr/local/lib/python3.6/site-packages/pika-1.0.0b1-py3.6.egg/pika/adapters/utils/selector_ioloop_adapter.py", line 610 in _dispatch_result
  File "/usr/local/lib/python3.6/site-packages/pika-1.0.0b1-py3.6.egg/pika/adapters/select_connection.py", line 457 in process_timeouts
  File "/usr/local/lib/python3.6/site-packages/pika-1.0.0b1-py3.6.egg/pika/adapters/select_connection.py", line 769 in start
  File "/usr/local/lib/python3.6/site-packages/pika-1.0.0b1-py3.6.egg/pika/adapters/select_connection.py", line 508 in start
  File "/usr/src/app/consumer/async_consumer.py", line 143 in reconnect
  File "/usr/local/lib/python3.6/site-packages/pika-1.0.0b1-py3.6.egg/pika/adapters/select_connection.py", line 297 in process_timeouts
  File "/usr/local/lib/python3.6/site-packages/pika-1.0.0b1-py3.6.egg/pika/adapters/select_connection.py", line 459 in process_timeouts
  File "/usr/local/lib/python3.6/site-packages/pika-1.0.0b1-py3.6.egg/pika/adapters/select_connection.py", line 769 in start
  File "/usr/local/lib/python3.6/site-packages/pika-1.0.0b1-py3.6.egg/pika/adapters/select_connection.py", line 508 in start
  File "/usr/src/app/consumer/async_consumer.py", line 143 in reconnect
  File "/usr/local/lib/python3.6/site-packages/pika-1.0.0b1-py3.6.egg/pika/adapters/select_connection.py", line 297 in process_timeouts
  File "/usr/local/lib/python3.6/site-packages/pika-1.0.0b1-py3.6.egg/pika/adapters/select_connection.py", line 459 in process_timeouts
  File "/usr/local/lib/python3.6/site-packages/pika-1.0.0b1-py3.6.egg/pika/adapters/select_connection.py", line 769 in start
  File "/usr/local/lib/python3.6/site-packages/pika-1.0.0b1-py3.6.egg/pika/adapters/select_connection.py", line 508 in start
  File "/usr/src/app/consumer/async_consumer.py", line 143 in reconnect

The last 5 lines are repeated a number of times and then finishes with

  ...
Aborted
> pip show pika
Name: pika
Version: 1.0.0b1
Summary: Pika Python AMQP Client Library
Home-page: https://pika.readthedocs.io
Author: None
Author-email: None
License: BSD
Location: /usr/local/lib/python3.6/site-packages/pika-1.0.0b1-py3.6.egg
Requires:
Required-by:
> python --version
Python 3.6.5

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:15 (11 by maintainers)

github_iconTop GitHub Comments

3reactions
lukebakkencommented, Feb 2, 2019

@Jacobh2 please see PR #1173, which fixes the reconnect example.

In short, calling back into the consumer class via call_later is fraught with the potential for a stack overflow. There is no way around this, of course, because the interaction between the ioloop and callbacks will always result in this scenario if call_later is used.

2reactions
lukebakkencommented, Feb 3, 2019

reconnect suffered from this issue because failed reconnect attempts would then call back into the same ExampleConsumer instance over and over.

So, It all depends on how add_callback_threadsafe is used. 99% of the time this method will be used to acknowledge a message and nothing else, so when the callback is done, it will return and be done.

If you have a specific example you’d like me to review I would be happy to. I will review all of the example code to ensure it does not suffer from the issue reported here.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Design patterns for asynchronous API communication
A client sends a request message through a topic to a consumer;; The consumer performs some action, then returns a response message through...
Read more >
Newest 'asynchronous' Questions - Stack Overflow
I am currently trying to convert a client script that is functioning synchronously to asynchronously. The goal of the script is to populate...
Read more >
Async/Await - Best Practices in Asynchronous Programming
Async void methods have different composing semantics. Async methods returning Task or Task<T> can be easily composed using await, Task.WhenAny, Task.WhenAll ...
Read more >
How can I implement a consumer-producer problem? [closed]
I want to implement consumer-producer problem in Mathematica. Example: I want 2 threads/processes: ... asynchronous-processing.
Read more >
Stack Overflow for Teams - Guided Demo - YouTube
Take a thorough guided walkthrough of Stack Overflow's knowledge-sharing platform, Stack Overflow for Teams.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found