Skip to content

Automatically close SQLite connection without using contextmanager #187

@JWCook

Description

@JWCook

Follow-up from #173.

Currently, the aiosqlite connection used by SQLiteBackend will only close automatically if used with the CachedSession contextmanager. Otherwise, the Thread object used by aiosqlite remains open and causes the program to hang (unlike the stdlib sqlite3 module, which works fine with leaving connections open).

Workarounds

Either use the contextmanager:

async with CachedSession(cache=SQLiteBackend()) as session:
    ...

Or explicitly call close():

session = CachedSession(cache=SQLiteBackend())
...
session.close()

Options

  1. Reopen and close connection for every request
    • Reliable, but comes with a performance hit.
  2. Schedule close using SQLiteBackend.__del__() (suggested here)
    • As @aaraney mentioned, this isn't guaranteed to complete before the event loop closes.
  3. Subclass and modify aiosqlite.Connection.run() to accept a signal to immediately close without blocking the event loop or needing to be scheduled
  4. Various other means of forcing the thread to close (like raising an exception inside the thread or messing with its internal queue)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingenhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions