Repository navigation
Emit resource warning if sqlite3 fails to close the database #105539
Description
Activity
- added3.13only security fixesonly security fixestype-featureA feature request or enhancementA feature request or enhancement
on Jun 8, 2023 What is the consequences if a database is not closed explicitly and fails to be closed at Python exit? Is there a risk of losing data?
What is the consequences if a database is not closed explicitly and fails to be closed at Python exit? Is there a risk of losing data?
If
sqlite3_close()orsqlite3_close_v2()fails, there is of course a risk of losing data. However, we do a pretty good job at cleaning up the connection and its debris (cursors, statements, registered callbacks, etc.), so that risk is almost non-existent :)By "fail to be closed", I was thinking about a database connection involved in a reference cycle which is impossible to break, and so close() is never called and the object is never destroyed.
By "fail to be closed", I was thinking about a database connection involved in a reference cycle which is impossible to break, and so close() is never called and the object is never destroyed.
If the connection object is never destroyed (dealloc or finalize are never called), there is no way to detect that in the extension module that I know about. The proposed change involves both the finalizer and the dealloc callbacks, so I'm relying on them actually being called at some point.
Congrats :-) IMO it's a nice enhancement.
Reacted by Erlend E. AaslandThanks for bringing the idea up in the first place, Victor! :)
Apparently, I did not cover all cases in #108017; the following now emit resource warnings:
./python.exe -m test -R : test_sqlite3 -m ClosedCurTests- linked a pull request that will close this issuegh-105539: Fix ResourceWarning from unclosed SQLite connection in ClosedCurTests. #108360
on Aug 23, 2023 - linked a pull request that will close this issuegh-105539: Emit ResourceWarning if sqlite3 database is not closed explicitly #108015
on Aug 23, 2023 - linked a pull request that will close this issuegh-105539: Explict resource management for connection objects in sqlite3 tests #108017
on Aug 23, 2023 - added a commit that references this issue
on Nov 10, 2025
Metadata
Metadata
Assignees
Labels
Projects
- StatusShow more project fieldsDone
See #103837 (comment) and #103837 (comment):
EAA:
VS:
Linked PRs