Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 35.2k
gh-108083: Don't ignore exceptions in sqlite3.Connection.__init__() and .close()#108084
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
Merged
erlend-aasland
merged 13 commits into
python:main
from
erlend-aasland:sqlite/do-not-ignore-exceptionsAug 18, 2023
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
0956795
gh-108083: Don't ignore exceptions in sqlite3.Connection.__init__() a…
erlend-aasland a5f6454
Cast
erlend-aasland 367f192
Pull in main
erlend-aasland 7b3d032
Address review: let connection_close() return a value and the caller …
erlend-aasland 379e64e
Don't log unraisable exceptions in case of interp shutdown
erlend-aasland 3905a88
Pull in main
erlend-aasland b2d49f3
Assert pre/post state in remove_callbacks()
erlend-aasland 0d4eb8b
Make sure we're not initialized if reinit fails
erlend-aasland 863f5b7
Try to close the database even if ROLLBACK fails
erlend-aasland ef5418a
Add comment
erlend-aasland b1b6d24
Pull in main
erlend-aasland 5d308aa
Reduce indent
erlend-aasland 8d57138
Also assert usage after set authorizer
erlend-aasland File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
3 changes: 3 additions & 0 deletions
3 Misc/NEWS.d/next/Library/2023-08-17-12-59-35.gh-issue-108083.9J7UcT.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| Fix bugs in the constructor of :mod:`sqlite3.Connection` and | ||
| :meth:`sqlite3.Connection.close` where exceptions could be leaked. Patch by | ||
| Erlend E. Aasland. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -149,7 +149,7 @@ static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self); | ||
| static void free_callback_context(callback_context *ctx); | ||
| static void set_callback_context(callback_context **ctx_pp, | ||
| callback_context *ctx); | ||
| static void connection_close(pysqlite_Connection *self); | ||
| static int connection_close(pysqlite_Connection *self); | ||
| PyObject *_pysqlite_query_execute(pysqlite_Cursor *, int, PyObject *, PyObject *); | ||
| static PyObject * | ||
| @@ -247,10 +247,13 @@ pysqlite_connection_init_impl(pysqlite_Connection *self, PyObject *database, | ||
| } | ||
| if (self->initialized) { | ||
| self->initialized = 0; | ||
| PyTypeObject *tp = Py_TYPE(self); | ||
| tp->tp_clear((PyObject *)self); | ||
| connection_close(self); | ||
| self->initialized = 0; | ||
| if (connection_close(self) < 0) { | ||
| return -1; | ||
| } | ||
| } | ||
| // Create and configure SQLite database object. | ||
| @@ -334,7 +337,9 @@ pysqlite_connection_init_impl(pysqlite_Connection *self, PyObject *database, | ||
| self->initialized = 1; | ||
| if (autocommit == AUTOCOMMIT_DISABLED) { | ||
| (void)connection_exec_stmt(self, "BEGIN"); | ||
| if (connection_exec_stmt(self, "BEGIN") < 0) { | ||
| return -1; | ||
| } | ||
| } | ||
| return 0; | ||
| @@ -432,48 +437,83 @@ free_callback_contexts(pysqlite_Connection *self) | ||
| static void | ||
| remove_callbacks(sqlite3 *db) | ||
| { | ||
| sqlite3_trace_v2(db, SQLITE_TRACE_STMT, 0, 0); | ||
| /* None of these APIs can fail, as long as they are given a valid | ||
| * database pointer. */ | ||
| assert(db != NULL); | ||
| int rc = sqlite3_trace_v2(db, SQLITE_TRACE_STMT, 0, 0); | ||
| assert(rc == SQLITE_OK), (void)rc; | ||
kumaraditya303 marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| sqlite3_progress_handler(db, 0, 0, (void *)0); | ||
| (void)sqlite3_set_authorizer(db, NULL, NULL); | ||
| rc = sqlite3_set_authorizer(db, NULL, NULL); | ||
| assert(rc == SQLITE_OK), (void)rc; | ||
| } | ||
| static void | ||
| static int | ||
| connection_close(pysqlite_Connection *self) | ||
| { | ||
| if (self->db) { | ||
| if (self->autocommit == AUTOCOMMIT_DISABLED && | ||
| !sqlite3_get_autocommit(self->db)) | ||
| { | ||
| /* If close is implicitly called as a result of interpreter | ||
| * tear-down, we must not call back into Python. */ | ||
| if (_Py_IsInterpreterFinalizing(PyInterpreterState_Get())) { | ||
| remove_callbacks(self->db); | ||
| } | ||
| (void)connection_exec_stmt(self, "ROLLBACK"); | ||
| if (self->db == NULL) { | ||
| return 0; | ||
| } | ||
| int rc = 0; | ||
| if (self->autocommit == AUTOCOMMIT_DISABLED && | ||
| !sqlite3_get_autocommit(self->db)) | ||
| { | ||
| if (connection_exec_stmt(self, "ROLLBACK") < 0) { | ||
| rc = -1; | ||
| } | ||
| } | ||
| free_callback_contexts(self); | ||
| sqlite3 *db = self->db; | ||
| self->db = NULL; | ||
| sqlite3 *db = self->db; | ||
| self->db = NULL; | ||
| Py_BEGIN_ALLOW_THREADS | ||
| /* The v2 close call always returns SQLITE_OK if given a valid database | ||
| * pointer (which we do), so we can safely ignore the return value */ | ||
| (void)sqlite3_close_v2(db); | ||
| Py_END_ALLOW_THREADS | ||
| Py_BEGIN_ALLOW_THREADS | ||
| int rc = sqlite3_close_v2(db); | ||
| assert(rc == SQLITE_OK), (void)rc; | ||
| Py_END_ALLOW_THREADS | ||
| } | ||
| free_callback_contexts(self); | ||
| return rc; | ||
| } | ||
| static void | ||
| connection_dealloc(pysqlite_Connection *self) | ||
| connection_finalize(PyObject *self) | ||
| { | ||
| PyTypeObject *tp = Py_TYPE(self); | ||
| PyObject_GC_UnTrack(self); | ||
| tp->tp_clear((PyObject *)self); | ||
| pysqlite_Connection *con = (pysqlite_Connection *)self; | ||
| PyObject *exc = PyErr_GetRaisedException(); | ||
| /* If close is implicitly called as a result of interpreter | ||
| * tear-down, we must not call back into Python. */ | ||
| PyInterpreterState *interp = PyInterpreterState_Get(); | ||
| int teardown = _Py_IsInterpreterFinalizing(interp); | ||
| if (teardown && con->db) { | ||
| remove_callbacks(con->db); | ||
erlend-aasland marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| } | ||
| /* Clean up if user has not called .close() explicitly. */ | ||
| connection_close(self); | ||
| if (connection_close(con) < 0) { | ||
| if (teardown) { | ||
| PyErr_Clear(); | ||
| } | ||
| else { | ||
| PyErr_WriteUnraisable((PyObject *)self); | ||
| } | ||
| } | ||
| PyErr_SetRaisedException(exc); | ||
| } | ||
| static void | ||
| connection_dealloc(PyObject *self) | ||
| { | ||
| if (PyObject_CallFinalizerFromDealloc(self) < 0) { | ||
| return; | ||
| } | ||
| PyTypeObject *tp = Py_TYPE(self); | ||
| PyObject_GC_UnTrack(self); | ||
| tp->tp_clear(self); | ||
| tp->tp_free(self); | ||
| Py_DECREF(tp); | ||
| } | ||
| @@ -621,7 +661,9 @@ pysqlite_connection_close_impl(pysqlite_Connection *self) | ||
| pysqlite_close_all_blobs(self); | ||
| Py_CLEAR(self->statement_cache); | ||
| connection_close(self); | ||
| if (connection_close(self) < 0) { | ||
| return NULL; | ||
| } | ||
| Py_RETURN_NONE; | ||
| } | ||
| @@ -2555,6 +2597,7 @@ static struct PyMemberDef connection_members[] = | ||
| }; | ||
| static PyType_Slot connection_slots[] = { | ||
| {Py_tp_finalize, connection_finalize}, | ||
| {Py_tp_dealloc, connection_dealloc}, | ||
| {Py_tp_doc, (void *)connection_doc}, | ||
| {Py_tp_methods, connection_methods}, | ||
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.