tortoise / tortoise/tortoise-orm
Getting `psycopg.OperationalError` constantly after a while on transactions
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 5.6k
- Forks
- 516
- Avg merge
- 2d 21h
- Merged PRs (30d)
- 9
Description
Describe the bug
I'm using FastAPI and TortoiseORM with psycopg to connect to a PostgreSQL database. It's been working great but after a while (2~3 days) I get constant psycopg.OperationalError: the connection is closed errors on every in_transaction()
To Reproduce
Basically just a async with in_transaction() block that gets constantly executed over a long period of time.
Expected behavior
For TortoiseORM to gracefully recover from connection errors if they happen on transactions.
Additional context
Using tortoise-orm[psycopg] version 0.25.1
Full traceback:
Traceback (most recent call last):
...
File "/app/client.py", line 84, in register_client
async with in_transaction():
~~~~~~~~~~~~~~^^
File "/app/.venv/lib/python3.13/site-packages/tortoise/backends/base/client.py", line 335, in __aenter__
await self.client.savepoint()
File "/app/.venv/lib/python3.13/site-packages/tortoise/backends/psycopg/client.py", line 230, in savepoint
return await self.begin()
^^^^^^^^^^^^^^^^^^
File "/app/.venv/lib/python3.13/site-packages/tortoise/backends/base_postgres/client.py", line 33, in _translate_exceptions
return await self._translate_exceptions(func, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/app/.venv/lib/python3.13/site-packages/tortoise/backends/psycopg/client.py", line 180, in _translate_exceptions
return await func(self, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/app/.venv/lib/python3.13/site-packages/tortoise/backends/psycopg/client.py", line 227, in begin
await self._transaction.__aenter__()
File "/usr/local/lib/python3.13/contextlib.py", line 214, in __aenter__
return await anext(self.gen)
^^^^^^^^^^^^^^^^^^^^^
File "/app/.venv/lib/python3.13/site-packages/psycopg/connection_async.py", line 360, in transaction
async with tx:
^^
File "/app/.venv/lib/python3.13/site-packages/psycopg/transaction.py", line 271, in __aenter__
await self._conn.wait(self._enter_gen())
File "/app/.venv/lib/python3.13/site-packages/psycopg/connection_async.py", line 487, in wait
return await waiting.wait_async(gen, self.pgconn.socket, interval=interval)
^^^^^^^^^^^^^^^^^^
File "psycopg_binary/pq/pgconn.pyx", line 205, in psycopg_binary.pq.PGconn.socket.__get__
File "psycopg_binary/pq/pgconn.pyx", line 718, in psycopg_binary.pq._call_int
File "psycopg_binary/pq/pgconn.pyx", line 698, in psycopg_binary.pq._ensure_pgconn
psycopg.OperationalError: the connection is closed
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with tortoise/backends/base/client.py and the PostgreSQL transaction paths shown in tortoise/backends/psycopg/client.py and tortoise/backends/base_postgres/client.py. Reproduce the long-running async in_transaction() case and trace how a closed connection reaches begin(). Done means transaction use recovers gracefully from the reported connection error, with coverage for the failure if the repository has relevant tests.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- fastapi, postgresql, python
- Domain
- databases
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 35/100