databases icon indicating copy to clipboard operation
databases copied to clipboard

Asyncpg pool close hanging and timeout

Open kriscode1 opened this issue 3 years ago • 0 comments

OS: Debian 10 Buster Python: 3.9 Databases: 0.5.3 Database backend: postgresql/asyncpg

There was an issue with our database and our app was hanging. We received this asyncpg warning:

Pool.close() is taking over 60 seconds to complete. Check if you have any unreleased connections left. Use asyncio.wait_for() to set a timeout for Pool.close().

That warning is raised from asyncpg's Pool.close() method. In line with their suggestion, I'd like to be able to set a timeout for this, or ideally all of the disconnect logic since an asyncpg timeout setting for this does not exist. My current (context manager only) workaround is approximately:

class DatabaseWithTimeout(Database):
    async def __aexit__(self, exc_type, exc_value, traceback):
        return await asyncio.wait_for(
            super().__aexit__(exc_type, exc_value, traceback),
            TIMEOUT_SECONDS
        )

But it would be nice if Databases had a similar feature built in. I'm happy to make a PR for this if it makes sense.

kriscode1 avatar Mar 17 '22 15:03 kriscode1