Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes leaking connections in _connect_addr #468

Merged
merged 2 commits into from
Nov 19, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions asyncpg/connect_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -508,9 +508,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 @@ -646,3 +652,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