Skip to content

Commit 58607ce

Browse files
author
Nikita Ilyasov
committed
1 parent ae5a89d commit 58607ce

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

asyncpg/connect_utils.py

+17-2
Original file line numberDiff line numberDiff line change
@@ -508,9 +508,15 @@ async def _connect_addr(*, addr, loop, timeout, params, config,
508508
else:
509509
connector = loop.create_connection(proto_factory, *addr)
510510

511+
connector = asyncio.ensure_future(connector)
512+
511513
before = time.monotonic()
512-
tr, pr = await asyncio.wait_for(
513-
connector, timeout=timeout, loop=loop)
514+
try:
515+
tr, pr = await asyncio.wait_for(
516+
connector, timeout=timeout, loop=loop)
517+
except asyncio.CancelledError:
518+
connector.add_done_callback(_close_leaked_connection)
519+
raise
514520
timeout -= time.monotonic() - before
515521

516522
try:
@@ -646,3 +652,12 @@ def _create_future(loop):
646652
return asyncio.Future(loop=loop)
647653
else:
648654
return create_future()
655+
656+
657+
def _close_leaked_connection(fut):
658+
try:
659+
tr, pr = fut.result()
660+
if tr:
661+
tr.close()
662+
except asyncio.CancelledError:
663+
pass # hide the exception

0 commit comments

Comments
 (0)