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

BUG: consumer_callbacks KeyError #132

Closed
mic1on opened this issue Jul 11, 2024 · 6 comments
Closed

BUG: consumer_callbacks KeyError #132

mic1on opened this issue Jul 11, 2024 · 6 comments
Labels
Milestone

Comments

@mic1on
Copy link

mic1on commented Jul 11, 2024

version: 2.10.7

I'm using the library in multithreading and I get the occasional KeyError.

I replicated the problem after troubleshooting and minimal units.

import threading

import amqpstorm

connection = amqpstorm.Connection(hostname="localhost",
                                  port=5672,
                                  username="admin",
                                  password="admin", )

channel = connection.channel()

queues = ["demo1", "demo2", "demo3"]

for queue in queues:
    channel.queue.declare(queue, durable=True)

    for i in range(100):
        channel.basic.publish(f"{i}", queue)


def start_consuming(queue):
    channel.basic.qos(prefetch_count=1)
    channel.basic.consume(
        queue=queue, callback=lambda x: x, no_ack=False
    )
    channel.start_consuming(to_tuple=False)


for queue in queues:
    t = threading.Thread(target=start_consuming, args=(queue,))
    t.start()

I forked the project and tried to fix it. see commit

But I think there may be a more appropriate solution

@mic1on
Copy link
Author

mic1on commented Jul 11, 2024

File "/Users/miclon/myproject/use-py/use-rabbitmq/example/demo2.py", line 26, in start_consuming
channel.start_consuming(to_tuple=False)
File "/Users/miclon/myproject/use-py/use-rabbitmq/.venv/lib/python3.11/site-packages/amqpstorm/channel.py", line 358, in start_consuming
self.process_data_events(
File "/Users/miclon/myproject/use-py/use-rabbitmq/.venv/lib/python3.11/site-packages/amqpstorm/channel.py", line 329, in process_data_events
self._consumer_callbacksconsumer_tag
~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^
KeyError: 'amq.ctag-MfRBvol6HqTpDreT-TNRPg'

@eandersson
Copy link
Owner

Interesting. I'll take a look, but a quick fix would be to have dedicated channel for each consumer, instead of sharing the same channel across multiple threads.

@eandersson eandersson added the bug label Jul 12, 2024
@eandersson
Copy link
Owner

When you have a chance can you take a look at this PR #134

@jhogg
Copy link
Collaborator

jhogg commented Jul 12, 2024 via email

@eandersson
Copy link
Owner

eandersson commented Jul 12, 2024

Erik,A concern, but as I read this, if prefetch=1 and acknowledgments are required, the continue if there is no consumer tag will hang that process.  Likewise, if acknowledgements are required it will hang at the point prefetch is exhausted with un-acked messages. JayOn Jul 12, 2024, at 5:20 AM, Erik Olof Gunnar Andersson @.> wrote: When you have a chance can you take a look at this PR #134 —Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you are subscribed to this thread.Message ID: @.>

The issue was a race condition where the code started consuming, and the other threads started pumping messages before the previous thread was done properly setting up the consumer. Since all of the IO happens asynchronously in the background and we were missing a lock protecting the setup phase.

@mic1on
Copy link
Author

mic1on commented Jul 12, 2024

@eandersson Thanks! Solved the problem I was having very quickly, hopefully a new version will be released soon!

eandersson added a commit that referenced this issue Jul 12, 2024
Fix issue consuming on the same channel across threads [#132]
@eandersson eandersson added this to the 2.10.8 milestone Jul 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants