tortoise / tortoise/tortoise-orm

Simultaneous get_or_create throws InterfaceError: Pool.release() received invalid connection

Open
#1,693 2 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
5.6k
Forks
516
Avg merge
2d 21h
Merged PRs (30d)
9

Description

An endpoint that uses get_or_create() is often called multiple times simultaneously (at the same millisecond). When the server starts, the first time this endpoint is called in this way one of these requests returns a 500 status:

"
File "/usr/local/lib/python3.12/site-packages/tortoise/models.py", line 1056, in get_or_create
async with in_transaction(connection_name=db.connection_name) as connection:
File "/usr/local/lib/python3.12/site-packages/tortoise/backends/base/client.py", line 283, in aexit
await self.connection._parent._pool.release(self.connection._connection)
File "/usr/local/lib/python3.12/site-packages/asyncpg/pool.py", line 841, in release
raise exceptions.InterfaceError(
asyncpg.exceptions._base.InterfaceError: Pool.release() received invalid connection: <PoolConnectionProxy <asyncpg.connection.Connection object at 0x7f8b065d0e60> 0x7f8b0654d1e0> is not a member of this pool
"

The other requests are successful. This issue only occurs if requests are sent at the same time.

Using:
fastapi==0.111.0
tortoise-orm==0.20.1
tortoise-vector==01.1
asyncpg==0.29.0

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The traceback points to tortoise/models.py:1056 and tortoise/backends/base/client.py:283, with asyncpg's Pool.release as the failing boundary. Reproduce the simultaneous get_or_create calls first; done means concurrent first requests no longer produce the invalid-connection InterfaceError and all requests succeed.

Written by the indexing model from the issue text.

Assessment

Tech stack
fastapi, postgresql, python
Domain
backend, database
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
32/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.