-
Notifications
You must be signed in to change notification settings - Fork 19
Close DB connection when Database is garbage collected #132
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
Close DB connection when Database is garbage collected #132
Conversation
Codecov ReportAttention: Patch coverage is
❗ Your organization needs to install the Codecov GitHub app to enable full functionality. Additional details and impacted files@@ Coverage Diff @@
## main #132 +/- ##
==========================================
- Coverage 99.34% 98.91% -0.43%
==========================================
Files 3 3
Lines 455 462 +7
==========================================
+ Hits 452 457 +5
- Misses 3 5 +2 ☔ View full report in Codecov by Sentry. |
f149a70
to
1844154
Compare
So this registers a cleanup function ( |
@mbhutton Thank you! Two minor things, otherwise LGTM.
|
1844154
to
d3d796e
Compare
Hi @mikez,
Yes I think so. The only options I can see are either:
If there were things.py clients which used large numbers of Database instances then one of the other options would be more appropriate, to force earlier cleanup, but I know this isn't an issue in practice based on the fact that this library has historically used a single connection. The reference to
I've added the following comment above the finalizer call:
Does that sound okay? |
@mbhutton Quick clarification on point 1. What I meant with referring to that citation in the docs was doing only the function call weakref.finalize(self, sqlite3.Connection.close, self.connection) as opposed to the assignment self._finalizer = weakref.finalize(self, sqlite3.Connection.close, self.connection) The strategy itself I like.
|
Register a weakref.finalize callback to close the underlying DB connection when the Database instance is garbage collected. This address a ResourceWarning which was introduced in Python 3.13 (python/cpython#105539).
d3d796e
to
dee457e
Compare
|
@mikez Thanks for clarifying, I believe that's everything resolved? |
@mbhutton Looks great to me! Thanks for your work here. 💛 |
Register a weakref.finalize callback to close the underlying DB connection when the Database instance is garbage collected.
This address a ResourceWarning which was introduced in Python 3.13 (python/cpython#105539).