diff --git a/asyncpg/pool.py b/asyncpg/pool.py index cd53e308..ed9ccff8 100644 --- a/asyncpg/pool.py +++ b/asyncpg/pool.py @@ -614,20 +614,19 @@ def _release_leaked_connection(fut): self._check_init() acquire_fut = asyncio.ensure_future(_acquire_impl()) - if timeout is None: - return await acquire_fut - else: - try: - return await asyncio.wait_for( - acquire_fut, timeout=timeout) - except asyncio.CancelledError: - # Ensure connection is marked as not in use. - # The cancellation may have raced the acquire, leading - # to the acquire completing but the wait_for to be - # cancelled. - # See: https://bugs.python.org/issue37658 - acquire_fut.add_done_callback(_release_leaked_connection) - raise + try: + # Calling wait_for with timeout=None will shortcut to run without + # timeout + return await asyncio.wait_for( + acquire_fut, timeout=timeout) + except asyncio.CancelledError: + # Ensure connection is marked as not in use. + # The cancellation may have raced the acquire, leading + # to the acquire completing but the wait_for to be + # cancelled. + # See: https://bugs.python.org/issue37658 + acquire_fut.add_done_callback(_release_leaked_connection) + raise async def release(self, connection, *, timeout=None): """Release a database connection back to the pool.