Skip to content

Commit f149a70

Browse files
committed
Close DB connection when Database is garbage collected
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).
1 parent a83d418 commit f149a70

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

things/database.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
import plistlib
99
import re
1010
import sqlite3
11+
import weakref
1112
from textwrap import dedent
1213
from typing import Optional, Union
1314

14-
1515
# --------------------------------------------------
1616
# Core constants
1717
# --------------------------------------------------
@@ -186,6 +186,9 @@ def __init__(self, filepath=None, print_sql=False):
186186
# See: https://sqlite.org/uri.html#recognized_query_parameters
187187
uri = f"file:{self.filepath}?mode=ro"
188188
self.connection = sqlite3.connect(uri, uri=True) # pylint: disable=E1101
189+
self._finalizer = weakref.finalize(
190+
self, sqlite3.Connection.close, self.connection
191+
)
189192

190193
# Test for migrated database in Things 3.15.16+
191194
# --------------------------------

0 commit comments

Comments
 (0)