Skip to content

gh-131918: Add _ThreadLocalSqliteConnection in dbm.sqlite #131920

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

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 36 additions & 7 deletions Lib/dbm/sqlite3.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from pathlib import Path
from contextlib import suppress, closing
from collections.abc import MutableMapping
from threading import current_thread
from weakref import ref

BUILD_TABLE = """
CREATE TABLE IF NOT EXISTS Dict (
Expand Down Expand Up @@ -32,6 +34,37 @@ def _normalize_uri(path):
uri = uri.replace("//", "/")
return uri

class _ThreadLocalSqliteConnection:
def __init__(self, uri: str):
self._uri = uri
self._conn = {}

def conn(self):
thread = current_thread()
idt = id(thread)
if idt in self._conn:
return self._conn[idt]
def thread_deleted(_, idt=idt):
# When the thread is deleted, remove the local dict.
# Note that this is suboptimal if the thread object gets
# caught in a reference loop. We would like to be called
# as soon as the OS-level thread ends instead.
if self._conn is not None:
conn = self._conn.pop(idt)
conn.close()
wrthread = ref(thread, thread_deleted)
try:
conn = sqlite3.connect(self._uri, autocommit=True, uri=True)
self._conn[id(thread)] = conn
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
self._conn[id(thread)] = conn
self._conn[thread.native_id] = conn

Hmm why not using native_id?

return conn
except sqlite3.Error as exc:
raise error(str(exc))

def close(self):
for t, conn in self._conn.items():
conn.close()
self._conn = {}


class _Database(MutableMapping):

Expand Down Expand Up @@ -59,15 +92,11 @@ def __init__(self, path, /, *, flag, mode):
# We use the URI format when opening the database.
uri = _normalize_uri(path)
uri = f"{uri}?mode={flag}"

try:
self._cx = sqlite3.connect(uri, autocommit=True, uri=True)
except sqlite3.Error as exc:
raise error(str(exc))
self._cx = _ThreadLocalSqliteConnection(uri)

# This is an optimization only; it's ok if it fails.
with suppress(sqlite3.OperationalError):
self._cx.execute("PRAGMA journal_mode = wal")
self._cx.conn().execute("PRAGMA journal_mode = wal")

if flag == "rwc":
self._execute(BUILD_TABLE)
Expand All @@ -76,7 +105,7 @@ def _execute(self, *args, **kwargs):
if not self._cx:
raise error(_ERR_CLOSED)
try:
return closing(self._cx.execute(*args, **kwargs))
return closing(self._cx.conn().execute(*args, **kwargs))
except sqlite3.Error as exc:
raise error(str(exc))

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixes dbm.sqlite3 for multi-threaded use-cases by using thread-local connections.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Fixes dbm.sqlite3 for multi-threaded use-cases by using thread-local connections.
Make :mod:`dbm.sqlite3` thread-safe by using thread-local database connections.

Loading