Skip to content

psycopg2.OperationalError: server closed the connection unexpectedly #372

@soar

Description

@soar

I'm using aiopg with SqlAlchemy support. All your examples looks like:

async with create_engine(...) as engine:
        async with engine.acquire() as conn:
            await conn.execute(...)

But I think this is wrong way: you shouldn't create_engine on every DB call. So engine object should be reusable instance in whole app. I've tried to use it like this:

# Somewhere on app init stage:
async def init_db_engine():
    db_engine = await aiopg.sa.create_engine(**engine_init_params)
    return db_engine

# Somewhere on app stop stage:
async def release_db_engine(db_engine):
    db_engine.close()
    return await db_engine.wait_closed()

# In some handlers:
async def get(*args, **kwargs):
    redirect_table = app.db.models.redirect.Redirect.__table__
    async with self.application.db.acquire() as conn:
        redirect_query = redirect_table.select().where(...)
        redirect_query_result = await conn.execute(redirect_query)
        redirect_info = await redirect_query_result.fetchone()

All looks like good, but... After some time of program inactivity any call to database fails with error:

	value = future.result()
	resp = yield from self._coro
	redirect_query_result = await conn.execute(redirect_query)
File "/usr/local/lib/python3.6/site-packages/aiopg/utils.py", line 72, in __await__
File "/usr/local/lib/python3.6/site-packages/aiopg/cursor.py", line 113, in execute
File "/usr/local/lib/python3.6/site-packages/aiopg/connection.py", line 237, in _poll
	yield from self._conn._poll(waiter, timeout)
	yield from cursor.execute(str(compiled), post_processed_params[0])
File "/usr/local/lib/python3.6/site-packages/aiopg/sa/connection.py", line 110, in _execute
	return fut.result()
File "/usr/local/lib/python3.6/asyncio/tasks.py", line 358, in wait_for
File "/usr/local/lib/python3.6/site-packages/aiopg/connection.py", line 134, in _ready
	yield from asyncio.wait_for(self._waiter, timeout, loop=self._loop)

psycopg2.OperationalError: server closed the connection unexpectedly before or while processing the request. This probably means the server terminated abnormally

Next call - works fine, so connection is reistablished.

What I've tried:

  1. Add parameter pool_recycle=1 to engine_init_params - it is not recognized
  2. Add minsize and maxsize parameters to engine_init_params - this didn't help

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions