Skip to content

Commit 2646c81

Browse files
committed
Update other use of wait_for workaround
Added issue link and comment describing why this workaround is necessary. Also moved the function to be local to the call site, as it it not used elsewhere.
1 parent 20511f8 commit 2646c81

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

asyncpg/connect_utils.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -615,12 +615,23 @@ async def _connect_addr(*, addr, loop, timeout, params, config,
615615
else:
616616
connector = loop.create_connection(proto_factory, *addr)
617617

618+
def _close_leaked_connection(fut):
619+
if not fut.cancelled():
620+
tr, pr = fut.result()
621+
if tr:
622+
tr.close()
623+
618624
connector = asyncio.ensure_future(connector)
619625
before = time.monotonic()
620626
try:
621627
tr, pr = await asyncio.wait_for(
622628
connector, timeout=timeout)
623629
except asyncio.CancelledError:
630+
# Ensure connection is closed.
631+
# The cancellation may have raced the connect, leading
632+
# to the connect completing but the wait_for to be
633+
# cancelled.
634+
# See: https://bugs.python.org/issue37658
624635
connector.add_done_callback(_close_leaked_connection)
625636
raise
626637
timeout -= time.monotonic() - before
@@ -721,12 +732,3 @@ def _create_future(loop):
721732
return asyncio.Future(loop=loop)
722733
else:
723734
return create_future()
724-
725-
726-
def _close_leaked_connection(fut):
727-
try:
728-
tr, pr = fut.result()
729-
if tr:
730-
tr.close()
731-
except asyncio.CancelledError:
732-
pass # hide the exception

0 commit comments

Comments
 (0)