Close DB connection when Database is garbage collected#132
Close DB connection when Database is garbage collected#132mikez merged 1 commit intothingsapi:mainfrom mbhutton:fix/close-db-connection-on-weakref-finalize
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. |
|
So this registers a cleanup function ( |
|
@mbhutton Thank you! Two minor things, otherwise LGTM.
|
|
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).
|
|
@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).