Skip to content

Commit e0ffe76

Browse files
author
Erlend E. Aasland
committed
Try to harden connection close
- add wrapper for sqlite3_close_v2() - explicitly free pending statements before close()
1 parent b53aacb commit e0ffe76

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

Modules/_sqlite/connection.c

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,19 @@ connection_clear(pysqlite_Connection *self)
250250
return 0;
251251
}
252252

253+
static int
254+
connection_close(pysqlite_Connection *self)
255+
{
256+
int rc = SQLITE_OK;
257+
258+
if (self->db) {
259+
rc = sqlite3_close_v2(self->db);
260+
self->db = NULL;
261+
}
262+
263+
return rc;
264+
}
265+
253266
static void
254267
connection_dealloc(pysqlite_Connection *self)
255268
{
@@ -258,9 +271,7 @@ connection_dealloc(pysqlite_Connection *self)
258271
tp->tp_clear((PyObject *)self);
259272

260273
/* Clean up if user has not called .close() explicitly. */
261-
if (self->db) {
262-
sqlite3_close_v2(self->db);
263-
}
274+
(void)connection_close(self);
264275

265276
tp->tp_free(self);
266277
Py_DECREF(tp);
@@ -345,22 +356,21 @@ static PyObject *
345356
pysqlite_connection_close_impl(pysqlite_Connection *self)
346357
/*[clinic end generated code: output=a546a0da212c9b97 input=3d58064bbffaa3d3]*/
347358
{
348-
int rc;
349-
350359
if (!pysqlite_check_thread(self)) {
351360
return NULL;
352361
}
353362

354-
pysqlite_do_all_statements(self, ACTION_FINALIZE, 1);
355-
356363
if (self->db) {
357-
rc = sqlite3_close_v2(self->db);
364+
/* Free pending statements before closing. This implies also cleaning
365+
* up cursors, as they may have strong refs to statements. */
366+
Py_CLEAR(self->statement_cache);
367+
Py_CLEAR(self->statements);
368+
Py_CLEAR(self->cursors);
358369

370+
int rc = connection_close(self);
359371
if (rc != SQLITE_OK) {
360372
_pysqlite_seterror(self->db);
361373
return NULL;
362-
} else {
363-
self->db = NULL;
364374
}
365375
}
366376

0 commit comments

Comments
 (0)