mosquito / aiormq

Pure python AMQP 0.9.1 asynchronous client library
Other
268 stars 58 forks source link

Channel number too large exception #34

Closed caiohsramos closed 5 years ago

caiohsramos commented 5 years ago

Hey, I'm running this code:

import asyncio

import aio_pika

conn = None

async def ping_rabbit():
    channel = await conn.channel()
    await channel.close()

async def main():
    global conn
    conn = await aio_pika.connect_robust('amqp://')
    req = []
    for _ in range(65535):
        req.append(ping_rabbit())
    await asyncio.gather(*req)
    print('Done')
    await ping_rabbit()

if __name__ == '__main__':
    asyncio.run(main())

and I'm getting "Channel number too large" exception right after the "Done". Probably something related to the file https://github.com/mosquito/aiormq/blob/master/aiormq/connection.py#L429. Could you guys take a look?

mosquito commented 5 years ago

Please provide some information about your environment (library version etc.)

caiohsramos commented 5 years ago

Name: aiormq Version: 2.5.2

Name: aio-pika Version: 5.5.2

RabbitMQ 3.6.12, Erlang 18.3

Python 3.7.0

caiohsramos commented 5 years ago

Maybe the logic here is not working in this case

if channel_number is None:
    async with self.last_channel_lock:
        # self.last_channel is 65534
        self.last_channel += 1
        # self.last_channel is now 65535

        # self.channels.keys() goes from 1 to 65534, so it does not enter while loop
        while self.last_channel in self.channels.keys():
            self.last_channel += 1

            if self.last_channel > 65535:
                log.warning("Resetting channel number for %r", self)
                self.last_channel = 1
                # switching context for prevent blocking event-loop
                await asyncio.sleep(0, loop=self.loop)

        channel_number = self.last_channel

Perhaps removing the first self.last_channel += 1 could solve this?

caiohsramos commented 5 years ago

Hey @mosquito, can we get a new version with this fix?

mosquito commented 5 years ago

@caiohsramos could you please check aiormq==2.5.2?

mosquito commented 5 years ago

@caiohsramos OMG my mistake. I already released the aiormq==2.5.3. Could you please test with it?

caiohsramos commented 5 years ago

@mosquito sorry to bother, but I don't think there is a 2.5.3 version on PyPI

mosquito commented 5 years ago

I am back from my vacation, and now I returning to the accumulated requests and issues. Thanks for your contribution.

Now the aiormq==2.5.3 was uploaded to pypi. Sorry for this mess.