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

Connections not closing when async tasks are cancelled #259

Open
PartlyAtomic opened this issue Sep 28, 2023 · 2 comments
Open

Connections not closing when async tasks are cancelled #259

PartlyAtomic opened this issue Sep 28, 2023 · 2 comments

Comments

@PartlyAtomic
Copy link

Description

When a task is cancelled while an aiosqlite.Connection object is connecting, the sqlite connection is never closed and is left hanging. Additionally the aiosqlite.Connection threads don't get shut down.

Details

  • OS: Windows 10 Pro 10.0.19045
  • Python version: 3.11.5
  • aiosqlite version: 0.19.0
  • Can you repro on 'main' branch? Yes
  • Can you repro in a clean virtualenv? Yes

Repro code:

import asyncio

import aiosqlite


async def connect_to_db():
    async with aiosqlite.connect("test.db") as db:
        await db.execute("SELECT value FROM generate_series(0,1000000,1)")


async def main_loop():
    tasks = []

    for i in range(5):
        tasks.append(asyncio.create_task(connect_to_db()))
    print("Tasks started")

    await asyncio.sleep(0)
    print("Async tick")

    for task in tasks:
        task.cancel()
    print("Tasks cancelled")

    for task in tasks:
        try:
            await task
        except asyncio.CancelledError:
            continue

    # This was a debug helper to track open connections
    # from aiosqlite.core import print_connections_info
    # print_connections_info()

    await asyncio.sleep(5)
    print("Complete")


if __name__ == "__main__":
    asyncio.run(main_loop())

Expected behavior: File handles for test.db would be released as tasks were successfully cancelled.

Actual behavior: 5 file handles are kept indefinitely for test.db (can be verified with a program such as OpenedFilesView or keeping track of the sqlite connections). Additionally after the event loop stops, the script hangs.

image

@PartlyAtomic
Copy link
Author

I suspect there may be other problems when tasks are cancelled, but haven't figured out minimal test cases for them yet. My program was often getting into a state where a series of cancelled write tasks would eventually lock the database indefinitely.

@iamthebull
Copy link

You may not be handling the CancelledError properly. See here.

This exception can be caught to perform custom operations when asyncio Tasks are cancelled. In almost all situations the exception must be re-raised.

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

2 participants