Skip to content

Getting psycopg.OperationalError constantly after a while on transactions #2007

@misa-hase

Description

@misa-hase

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 

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions