Skip to content

Commit

Permalink
Use asyncio.wait_for in pool acquire regardless of timeout
Browse files Browse the repository at this point in the history
When wait_for is called with timeout=None, it runs without
a timeout, as desired
  • Loading branch information
aaliddell committed May 2, 2020
1 parent 6f22ad8 commit 51b8d7c
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions asyncpg/pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 51b8d7c

Please sign in to comment.