Skip to content

Leaked connections caused by asyncio.CancelledError #464

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

Closed
nevladov opened this issue Jul 11, 2019 · 3 comments
Closed

Leaked connections caused by asyncio.CancelledError #464

nevladov opened this issue Jul 11, 2019 · 3 comments

Comments

@nevladov
Copy link

nevladov commented Jul 11, 2019

  • asyncpg version: 0.18.2
  • PostgreSQL version: 10
  • Do you use a PostgreSQL SaaS? If so, which? Can you reproduce
    the issue with a local PostgreSQL install?
    :
  • Python version: 3.6.6
  • Platform:
  • Do you use pgbouncer?: yes
  • Did you install asyncpg with pip?:
  • If you built asyncpg locally, which version of Cython did you use?:
  • Can the issue be reproduced under both asyncio and
    uvloop?
    :

asyncio.CancelledError occured in this place may be cause of connection leak.
The code below reproduces this problem:

import asyncpg
import traceback

async def test_connect():
    conn = None
    try:
        print('started')
        conn = await asyncpg.connect(
            host='your_host',
            port='6432',
            database='db',
            user='user',
            password='password',
            ssl=True,
        )
    except asyncio.CancelledError:
        print('CancelledError occured')
        traceback.print_exc()
    finally:
        if conn:
            await conn.close()
            print('connection closed')
            return
        print('connection was not closed, maybe leaked')


async def test_cancel():
    for i in range(50): # try 50 times
        future = asyncio.ensure_future(test_connect())
        await asyncio.sleep(0.09) # this sleep allows to call connect
        future.cancel()
    await asyncio.sleep(600)


import asyncio
loop = asyncio.get_event_loop()
loop.run_until_complete(test_cancel())

Printed traceback:

Traceback (most recent call last):
  File "<stdin>", line 11, in test_connect
  File "python3.6/site-packages/asyncpg/connection.py", line 1688, in connect
    max_cacheable_statement_size=max_cacheable_statement_size)
  File "python3.6/site-packages/asyncpg/connect_utils.py", line 543, in _connect
    connection_class=connection_class)
  File "python3.6/site-packages/asyncpg/connect_utils.py", line 519, in _connect_addr
    await asyncio.wait_for(connected, loop=loop, timeout=timeout)
  File "python3.6/asyncio/tasks.py", line 351, in wait_for
    yield from waiter
concurrent.futures._base.CancelledError

After running this script 3-6 iterations of 50 lead to connection leaks:

$ netstat -a | grep 6432 |  awk '{ print $6 }'
ESTABLISHED
ESTABLISHED
ESTABLISHED
ESTABLISHED

These connections remain establised while python process is running.
Our service is based on aiohttp application. Servise has about 5K request per second. When client request is finished by timeout aiohttp cancels handler task. This situation occurrs very often when we have network problem. And in this case we have thousands of lost connections.

@stachlewski
Copy link

With aiohttp you need to shield() your code. Connect to the database inside a shielded function.

@1st1
Copy link
Member

1st1 commented Nov 19, 2019

Should be fixed in master. Thanks for reporting!

@elprans
Copy link
Member

elprans commented Nov 26, 2020

Fixed in #608

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants