question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItĀ collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to do more than one cleanup task in cancelled state?

See original GitHub issue
  • asyncpg version: 0.23.0
  • PostgreSQL version: 12.6
  • Do you use a PostgreSQL SaaS? If so, which? Can you reproduce the issue with a local PostgreSQL install?: no/ yes
  • Python version:3.9.5
  • Platform: Fedora Linux
  • Do you use pgbouncer?: no
  • Did you install asyncpg with pip?: yes
  • If you built asyncpg locally, which version of Cython did you use?: n/a
  • Can the issue be reproduced under both asyncio and uvloop?: have only tested w/ asyncio directly

We have a user illustrating the case that a task being cancelled will allow us to reach a finally: block where we can at most run only one awaitable cleanup task on asyncpg in order to close out the connection. if we have more than one thing to await, such as emitting a ROLLBACK or anything else, we don’t get the chance to close() the connection. It then seems to go into some place where we no longer have any reference to this connection yet asyncpg still leaves it opened; our own GC handlers that are supposed to take care of this are never called.

One way to illustrate it is the use case in such a way that indicates how I’m looking for ā€œhow to solve this problem?ā€, of having two separate asycnpg connections that suppose we are doing some kind of work on separately in the same awaitable. if a cancel() is called, I can reach the finally: block, and I can then close at most one of the connections, but not both. in the real case, we are using only one connection but we are trying to emit a ROLLBACK and also do other awaitable things before we get to the .close().

What I dont understand is why gc isn’t collecting these connections or why they aren’t getting closed.

import asyncio

from asyncio import current_task
import asyncpg


async def get_and_cancel():
    c1 = await asyncpg.connect(
        user="scott", password="tiger", host="localhost", database="test"
    )
    c2 = await asyncpg.connect(
        user="scott", password="tiger", host="localhost", database="test"
    )
    try:
        r1 = await c1.fetch("SELECT 1")
        r2 = await c2.fetch("SELECT 1")
        current_task().cancel()
    finally:

        # we get here...

        # this seems to affect the asyncpg connection, the await is
        # honored....
        await c1.close()

        # but we never get here. connection leaks.  canonical way to
        # solve this issue?  
        await c2.close()


async def main():
    while True:
        try:
            await get_and_cancel()
        except asyncio.exceptions.CancelledError:
            pass


asyncio.run(main())

the stack trace is that we’ve run out of connections:

Traceback (most recent call last):
  File "/home/classic/dev/sqlalchemy/test4.py", line 39, in <module>
    asyncio.run(main())
  File "/usr/lib64/python3.9/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/usr/lib64/python3.9/asyncio/base_events.py", line 642, in run_until_complete
    return future.result()
  File "/home/classic/dev/sqlalchemy/test4.py", line 34, in main
    await get_and_cancel()
  File "/home/classic/dev/sqlalchemy/test4.py", line 11, in get_and_cancel
    c2 = await asyncpg.connect(
  File "/home/classic/.venv3/lib64/python3.9/site-packages/asyncpg/connection.py", line 1981, in connect
    return await connect_utils._connect(
  File "/home/classic/.venv3/lib64/python3.9/site-packages/asyncpg/connect_utils.py", line 732, in _connect
    con = await _connect_addr(
  File "/home/classic/.venv3/lib64/python3.9/site-packages/asyncpg/connect_utils.py", line 632, in _connect_addr
    return await __connect_addr(params, timeout, True, *args)
  File "/home/classic/.venv3/lib64/python3.9/site-packages/asyncpg/connect_utils.py", line 682, in __connect_addr
    await compat.wait_for(connected, timeout=timeout)
  File "/home/classic/.venv3/lib64/python3.9/site-packages/asyncpg/compat.py", line 103, in wait_for
    return await asyncio.wait_for(fut, timeout)
  File "/usr/lib64/python3.9/asyncio/tasks.py", line 481, in wait_for
    return fut.result()
asyncpg.exceptions.TooManyConnectionsError: remaining connection slots are reserved for non-replication superuser connections

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
fantixcommented, Jun 19, 2021

Aha, looks like there is an asyncio timer handle (300s) to clear some statement cache (_on_entry_expired) that has a callback handle _on_remove which is a bound method of the connection object - let me try fixing that.

0reactions
fantixcommented, Jun 18, 2021

Ah I see what you mean. Looks like without the cancellation, the connection will be recycled properly. šŸ¤” Looking into it now


Hmm no, as soon as the the connection is used, it’s not recycled without an explicit close() šŸ¤”

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to correctly clean up after long running task is cancelled
I've created a class whose purpose is to abstract away the control of concurrent access to a queue. The class is designed to...
Read more >
Task Cancellation | Microsoft Learn
The preferred way to perform is to use the ThrowIfCancellationRequested method. A task that's canceled in this way transitions to the Canceled ......
Read more >
Cleanup Submitted Tasks Tab - TechDocs - Broadcom Inc.
Specifies the minimum age of tasks that are in a final state (Completed, Failed, Rejected, Cancelled, or Aborted) to clean up. For example,...
Read more >
Satellite: How a stuck task can be removed. Always open a ...
The task to remove the repository failed due to the old locks. I opened a support case and recorded the commands performed by...
Read more >
Task cancellation in C# and things you should know about it
CancellationToken – a structure used by listeners to monitor token current state. How do we pass token. First of all, we should somehow...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found