Skip to content

Commit cf13208

Browse files
committed
gh-160699: use serialized SQLite connections
1 parent d3450fa commit cf13208

1 file changed

Lines changed: 9 additions & 3 deletions

File tree

‎Modules/_sqlite/connection.c‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,10 +261,16 @@ pysqlite_connection_init_impl(pysqlite_Connection *self, PyObject *database,
261261
// Create and configure SQLite database object.
262262
sqlite3 *db;
263263
int rc;
264+
int open_flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
265+
(uri ? SQLITE_OPEN_URI : 0);
266+
if (sqlite3_threadsafe()) {
267+
// The compile-time default may be multi-thread mode. Force each
268+
// connection to serialize concurrent use, as check_same_thread=False
269+
// permits sharing one connection between threads.
270+
open_flags |= SQLITE_OPEN_FULLMUTEX;
271+
}
264272
Py_BEGIN_ALLOW_THREADS
265-
rc = sqlite3_open_v2(PyBytes_AS_STRING(bytes), &db,
266-
SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
267-
(uri ? SQLITE_OPEN_URI : 0), NULL);
273+
rc = sqlite3_open_v2(PyBytes_AS_STRING(bytes), &db, open_flags, NULL);
268274
if (rc == SQLITE_OK) {
269275
(void)sqlite3_busy_timeout(db, (int)(timeout*1000));
270276
}

0 commit comments

Comments
 (0)