Skip to content

Pool.close() does not close the pool gracefully #290

@tomalexander

Description

@tomalexander
  • asyncpg version: 0.15.0
  • PostgreSQL version: 9.5.10
  • Do you use a PostgreSQL SaaS? If so, which? Can you reproduce
    the issue with a local PostgreSQL install?
    : using local
  • Python version: 3.6.5
  • Platform: Arch Linux
  • Do you use pgbouncer?: No
  • Did you install asyncpg with pip?: Yes
  • If you built asyncpg locally, which version of Cython did you use?:
  • Can the issue be reproduced under both asyncio and
    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 by Pool.close() setting self._closed immediately, which makes any call to Pool.release() fail because it immediately calls _check_init() which checks the value of the self._closed boolean and throws an exception.

Minimal Example

#!/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())

Output from running example

$ ./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 

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