#!/usr/bin/env python3
#
import asyncio
from asyncpg import create_pool
async def get_pool():
new_pool = await create_pool(
'postgresql://localhost:5432/postgres',
min_size=1,
max_size=4,
loop=loop,
)
return new_pool
async def hold_connection_open():
async with pool.acquire() as connection:
async with connection.transaction():
connection_has_been_opened.set_result(True)
asyncio.sleep(3)
async def wait_then_close():
await connection_has_been_opened
await pool.close()
loop = asyncio.get_event_loop()
pool = loop.run_until_complete(get_pool())
connection_has_been_opened = asyncio.Future()
asyncio.ensure_future(hold_connection_open())
loop.run_until_complete(wait_then_close())
$ ./bug.py
Task exception was never retrieved
future: <Task finished coro=<hold_connection_open() done, defined at ./bug.py:21> exception=InterfaceError('pool is closed',)>
Traceback (most recent call last):
File "./bug.py", line 25, in hold_connection_open
asyncio.sleep(3)
File "/home/talexander/.virtualenvs/dynamic_async_postgres/lib/python3.6/site-packages/asyncpg/transaction.py", line 91, in __aexit__
await self.__commit()
File "/home/talexander/.virtualenvs/dynamic_async_postgres/lib/python3.6/site-packages/asyncpg/transaction.py", line 179, in __commit
await self._connection.execute(query)
File "/home/talexander/.virtualenvs/dynamic_async_postgres/lib/python3.6/site-packages/asyncpg/connection.py", line 238, in execute
return await self._protocol.query(query, timeout)
File "asyncpg/protocol/protocol.pyx", line 319, in query
File "asyncpg/protocol/protocol.pyx", line 796, in asyncpg.protocol.protocol.BaseProtocol._dispatch_result
asyncpg.exceptions._base.InternalClientError: got result for unknown protocol state 3
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "./bug.py", line 25, in hold_connection_open
asyncio.sleep(3)
File "/home/talexander/.virtualenvs/dynamic_async_postgres/lib/python3.6/site-packages/asyncpg/pool.py", line 583, in __aexit__
await self.pool.release(con)
File "/home/talexander/.virtualenvs/dynamic_async_postgres/lib/python3.6/site-packages/asyncpg/pool.py", line 497, in release
self._check_init()
File "/home/talexander/.virtualenvs/dynamic_async_postgres/lib/python3.6/site-packages/asyncpg/pool.py", line 544, in _check_init
raise exceptions.InterfaceError('pool is closed')
asyncpg.exceptions._base.InterfaceError: pool is closed
the issue with a local PostgreSQL install?: using local
uvloop?: Yes
The documentation for
Pool.close()says that it "Gracefully close all connections in the pool." however if any connection is open then it throws an exception which seems not graceful. I've pasted a minimal example below demonstrating the issue but I believe its caused byPool.close()settingself._closedimmediately, which makes any call toPool.release()fail because it immediately calls_check_init()which checks the value of theself._closedboolean and throws an exception.Minimal Example
Output from running example