MagicStack / asyncpg

A fast PostgreSQL Database Client Library for Python/asyncio.
Apache License 2.0
6.99k stars 404 forks source link

asyncpg.exceptions._base.InterfaceError: cannot call Connection.fetch(): connection has been released back to the pool #988

Open iyuhang opened 1 year ago

iyuhang commented 1 year ago

I am using Sanic + tortoise-orm,

the test code as below:

image

when runs a query during the Request handler, it runs successfully

while runs in a background task, it report this error:

asyncpg.exceptions._base.InterfaceError: cannot call Connection.fetch(): connection has been released back to the pool

I found when the first time query, through the pool.acquire, got the right <PoolConnectionProxy <asyncpg.connection.Connection object at 0x7fd42102f9e0> 0x7fd3d018e550>

but when go to the background task query , the pool.acquire, got an "released" <PoolConnectionProxy [released] 0x7fd3d018e550>

I am confused how can I got an released PoolConnectionProxy from the same code

[query in the get Request method]

query in get

[query in the background task]

query in task

[the pool connection acquire method]

acquire

[the pool connection release method]

release
VyachAp commented 1 year ago

Faced the same issue. Any solutions here?

teleportx commented 8 months ago

Same issue

larsblumberg commented 2 months ago

Hi @iyuhang, thanks for giving this detailed report. Using another web framework (Quart) with asyncpg I am running into exactly the same issue: A background task is constantly acquiring released connections, resulting in

InterfaceError

cannot call Connection.fetch(): connection has been released back to the pool

Noteworthy to say that this behavior, keeps appearing and disappearing: My background task periodically runs a certain query every some minutes. And there are long streaks where all just works fine. And then there are periods, where all connection attempts fail due to this "released connection" issue.

It seems it has to do with sharing the same asyncpg connection pool across different Tasks. Could there be a bug in asyncpg?

mehdinourollah commented 1 month ago

any update here ?