Polyconseil / aioamqp

AMQP implementation using asyncio
Other
280 stars 88 forks source link

How to reconnect channel after aioamqp.exceptions.ChannelClosed? #183

Closed Jacobh2 closed 5 years ago

Jacobh2 commented 5 years ago

Hey!

This is not a bug of the lib per say, but a question about a problem I'm facing. Sorry if this is the wrong place to ask this.

I'm getting aioamqp.exceptions.ChannelClosed being thrown at me after some time when I do channel.basic_client_ack.

I'm not 100% why the channel is getting closed, the protocol.ensure_open() doesn't throw any errors, so it seems to still have some connection to the broker.

My idea was to simply recreate the channel object by running

channel = await protocol.channel()
await channel.exchange_declare(EXCHANGE, EXCHANGE_TYPE)
queue = await channel.queue_declare(
    queue_name=QUEUE_NAME,
    durable=True,
    arguments={
        'x-message-ttl': MESSAGE_TTL,
        'x-max-priority': DEFAULT_MESSAGE_PRIORITY
    }
)
await channel.basic_qos(prefetch_count=QUEUE_PREFETCH)
await channel.queue_bind(QUEUE_NAME, EXCHANGE, ROUTING_KEY)
await channel.basic_consume(on_message, queue_name=QUEUE_NAME)

on the same protocol instance, which seems to still be open and working.

This seems to now be the way to go, since I then see the following in the broker's logs (unknown delivery tag):

2018-12-07 07:34:34.346 [error] <0.11836.5> Channel error on connection <0.10381.5> (client_sender_1.client_default:50362 -> testhost:5672, vhost: '/', user: 'guest'), channel 1: operation basic.ack caused a channel exception precondition_failed: unknown delivery tag 160
2018-12-07 07:34:34.801 [error] <0.11843.5> Channel error on connection <0.10381.5> (client_sender_1.client_default:50362 -> testhost:5672, vhost: '/', user: 'guest'), channel 1: operation basic.ack caused a channel exception precondition_failed: unknown delivery tag 200
2018-12-07 07:34:35.091 [error] <0.11850.5> Channel error on connection <0.10381.5> (client_sender_1.client_default:50362 -> testhost:5672, vhost: '/', user: 'guest'), channel 1: operation basic.ack caused a channel exception precondition_failed: unknown delivery tag 60

My thought was then that when I recreate the channel, it gets a new channel_id, which might throw it off, but it seems like by looking in the broker logs, that it is always channel 1 that is being used.

Jacobh2 commented 5 years ago

Sorry for the clutter, this question is not relevant: I would say the simple answer is that you don't reopen a closed channel.

My problem is really not how to reopen a closed channel, but why the channel gets closed in the first place!