mosquito / aio-pika

AMQP 0.9 client designed for asyncio and humans.
https://aio-pika.readthedocs.org/
Apache License 2.0
1.23k stars 187 forks source link

Callback error from RPC example #361

Open hyumo opened 3 years ago

hyumo commented 3 years ago

Hi,

Thanks for creating and maintaining this library. I am trying to utilize the RPC pattern for a project, however, I am encountering the error below when running the caller/callee RPC example. Greatly appreciated if this could be fixed. Thanks.

Callback error
Traceback (most recent call last):
  File "/home/hyu/miniconda3/envs/fmpy/lib/python3.8/site-packages/aio_pika/tools.py", line 166, in __call__
    cb(self.__sender(), *args, **kwargs)
TypeError: on_close() takes from 1 to 2 positional arguments but 3 were given
mosquito commented 3 years ago

@hyumo Please provide aio-pika and aiormq versions

12lol12lol12lol commented 3 years ago

I have the same proble aio-pika==6.7.1 aiormq==3.3.1

smichalo commented 3 years ago

same here, same versions

hyumo commented 3 years ago

Same versions on Windows 10, 64bit

@12lol12lol12lol , @smichalo I ended up fixing the issue by closing the connection manually instead of using the with method. I guess this avoids the on_close method which is causing the issue.

12lol12lol12lol commented 3 years ago

@hyumo Thank you. It helped me

MorningLightMountain713 commented 3 years ago

Change:

patterns/rpc.py

from:

def on_close(self, exc=None):

to:

def on_close(self, exc=None, *args, **kwargs):

viv-1 commented 3 years ago

Hi,

This problem is still present with aio-pika==6.8.0 and aiormq==3.3.1.

I modified the on_close method on RPC class like this :

def on_close(self, exc, m):                                                 
      print(type(exc), m)

And I get :

<class 'aio_pika.robust_channel.RobustChannel'> None

It seems that the channel is passed as argument to on_change. According to API ref this is not intended. Do you have any fix for that ?

skrech commented 3 years ago

Hello,

I also confirm that this is a bug. CallbackCollection is executing all the callback with first argument self.__sender() which is not expected by on_close callback attached from RPC.

aio-pika: 6.8.0 aiormq: 3.3.1

Seems to be a problem from aio-pika only, not from aiormq.

maxims94 commented 3 years ago

I also ran into this issue. The fix is pretty simple: Just change

def on_close(self, exc=None):

To

def on_close(self, sender: "RPC", exc=None):

This is similar to how on_message_returned is defined.

As a workaround, you can avoid using the async context manager with the connection, as it closes the channels on exiting, which triggers the callback.