Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

psycopg2.OperationalError: server closed the connection unexpectedly #372

Closed
soar opened this issue Aug 28, 2017 · 3 comments
Closed

psycopg2.OperationalError: server closed the connection unexpectedly #372

soar opened this issue Aug 28, 2017 · 3 comments

Comments

@soar
Copy link
Contributor

soar commented Aug 28, 2017

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
@soar
Copy link
Contributor Author

soar commented Aug 28, 2017

I can't reproduce this issue on localhost (and/or) in debugger. But on real production environment in Docker, when DB and application running in different containers over network - it reproduces with 100% chance.

@asvetlov
Copy link
Member

Perhaps a connection from pool goes to wrong state, we should drop it.

@jettify
Copy link
Member

jettify commented Oct 17, 2017

I believe issue resolved #373

@jettify jettify closed this as completed Oct 17, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants