Skip to content

Commit

Permalink
Close transport if connection times out (#468)
Browse files Browse the repository at this point in the history
  • Loading branch information
hexrain authored and 1st1 committed Nov 19, 2019
1 parent 9cbca1c commit 926f483
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions asyncpg/connect_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,9 +526,15 @@ async def _connect_addr(*, addr, loop, timeout, params, config,
else:
connector = loop.create_connection(proto_factory, *addr)

connector = asyncio.ensure_future(connector)

before = time.monotonic()
tr, pr = await asyncio.wait_for(
connector, timeout=timeout, loop=loop)
try:
tr, pr = await asyncio.wait_for(
connector, timeout=timeout, loop=loop)
except asyncio.CancelledError:
connector.add_done_callback(_close_leaked_connection)
raise
timeout -= time.monotonic() - before

try:
Expand Down Expand Up @@ -664,3 +670,12 @@ def _create_future(loop):
return asyncio.Future(loop=loop)
else:
return create_future()


def _close_leaked_connection(fut):
try:
tr, pr = fut.result()
if tr:
tr.close()
except asyncio.CancelledError:
pass # hide the exception

0 comments on commit 926f483

Please sign in to comment.