Closed felipecwb closed 9 months ago
I fixed with:
async def consume(connection, queue_name) -> None:
# ... queue creating and consume...
# release this function with channel close
channel_closed_event = asyncio.Event()
queue.channel.close_callbacks.add(
lambda channel, exception: channel_closed_event.set()
)
try:
await channel_closed_event.wait()
finally:
await queue.cancel(consumer_tag)
async def main():
# ... await connection ...
while True:
try:
await asyncio.gather(
consume(connection, "testing.queue.a")
consume(connection, "testing.queue.b")
)
except:
logging.exception("exception")
await asyncio.sleep(5)
I just while and wait for channel to close after trigger consume
I'm also facing this issue.
It seems the connection is correctly recreated after a "Connection was stuck", but not the channels.
Steps to reproduce using two Docker containers, consumer and rabbitmq
docker network disconnect <network> <container>
docker network connect <network> <container>
Here is my consumer:
connection = await aio_pika.connect_robust(os.environ["RABBITMQ_CONN_STRING"], timeout=5)
async with connection:
# Creating channel
channel = await connection.channel()
# Declaring exchange
exchange = await channel.declare_exchange(
"my-exchange", type=ExchangeType.TOPIC, durable=True, auto_delete=False
)
# Declaring queue
queue = await channel.declare_queue("my-queue", durable=True, auto_delete=False)
# Binding queue to exchange
await queue.bind(exchange, "my-routing-key")
# Iterate over messages
async with queue.iterator() as queue_iter:
async for message in queue_iter:
try:
async with message.process():
logging.info(message.body.decode())
@sylvain1811 please provide aio-pika and aiormq versions
@mosquito Sure! Here it is:
aio-pika==9.1.4
aiormq==6.7.6
Python : 3.9
@sylvain1811 Please double check it with latest 9.2.2, this should be fixed in #571
@mosquito It's working fine with the latest release of aio-pika
(9.2.2), thank you!
@mosquito thank you for this amazing library!
I'm sorry to report that for me, even with aio-pika==9.2.2 and aiormq==6.7.7, the channels of a robust connection are not restored when a connection gets stuck. When closing the connection via RabbitMQ Management everything is restored correctly and the restoration of all resources is logged.
I can reproduce it by using the 2 docker containers and using the docker network disconnect|connect ......
procedure described by @sylvain1811:
missed heartbeats from client, timeout: 60s
but before aio-pika reports any stuck connection then it logs: Closing connection <Connection: "amqp://rabbitmq:******@rabbitmq:5672" at 0x7fc5c66cd810> cause: <AMQPConnectionError: ("Server connection reset: ConnectionResetError(104, 'Connection reset by peer')",)>
and the channels are restored correctly. I'm having the exact same issue (aio-pika:9.2.2, aiormq:6.7.7). After the Server connection <Connection: "amqp://guest:******@localhost:5672//" at 0x1048d6a30> was stuck. No frames were received in 183 seconds.
log, connection re-establishes however channels are not restored. When I attempt to consume or send a message after the connection is re-established I receive the ChannelInvalidStateError exceptions.
aiormq.exceptions.ChannelInvalidStateError: <Channel: "1" at 0x104c76da0> closed
aiormq.exceptions.ChannelInvalidStateError: Channel closed by RPC timeout
Has anyone found a solution yet?
Thanks to @aozupek the problem is resolved and a new fixed version released as aio-pika==9.2.3
@aozupek @mosquito with 9.2.3 it works like a charm. Thanks for your work!
I have a similar code for example:
Is working good when Rabbitmq Server got restarted and messages are processed again... but when I turn off WiFi and turn on again after the
connection.__heartbeat_grace_timeout
the reconnect "works" but I didn't received any new message.logs:
is there any way of
asyncio.Future
to raise an execption on this case?versions: