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

Automatically close SQLite connection without using contextmanager #187

Closed
JWCook opened this issue Oct 3, 2023 · 2 comments
Closed

Automatically close SQLite connection without using contextmanager #187

JWCook opened this issue Oct 3, 2023 · 2 comments
Labels
bug Something isn't working enhancement New feature or request
Milestone

Comments

@JWCook
Copy link
Member

JWCook commented Oct 3, 2023

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)
@JWCook
Copy link
Member Author

JWCook commented Oct 12, 2023

@aaraney Give the changes in main a try, and let me know if that resolves this issue for you.

@JWCook JWCook closed this as completed Oct 27, 2023
@JWCook
Copy link
Member Author

JWCook commented Oct 30, 2023

These changes are now available in v0.10.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant