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

Conversation

meghprkh
Copy link

@meghprkh meghprkh commented Mar 30, 2025

I am a first-time contributor and I am not sure about the quality of the code at all, review comments will be appreciated :)

Will try to add a test later. Should this be back-ported to Python 3.13?

@ghost
Copy link

ghost commented Mar 30, 2025

All commit authors signed the Contributor License Agreement.
CLA signed

@bedevere-app
Copy link

bedevere-app bot commented Mar 30, 2025

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@erlend-aasland
Copy link
Contributor

Should this be back-ported to Python 3.13?

IMO, yes.

@@ -0,0 +1 @@
Fixes dbm.sqlite3 for multi-threaded use-cases by using thread-local connections.
Copy link
Contributor

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.

@meghprkh
Copy link
Author

Hmm this fix doesnt really work with close, but is closing sqlite connections opened in WAL mode necesssary?

As in currently the close method closes connections from other threads, which would give the same error.

@brianschubert
Copy link
Contributor

brianschubert commented Mar 30, 2025

Not too familiar with sqlite3 thread-safety, but does this need to consider the case where sqlite3.threadsafety is 0, which (I think) would make forming connections from multiple threads unsafe?

There should probably be a docs update to explain exactly what thread-safety guarantees this is making (and if they depend on sqlite3.threadsafety?)

@meghprkh
Copy link
Author

Very weirdly, on my system (Fedora Linux), the sqlite3.threadsafety is set to 3 by default, but I still seem to be getting the error

@erlend-aasland erlend-aasland marked this pull request as draft March 31, 2025 06:01
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?

@python-cla-bot
Copy link

python-cla-bot bot commented Apr 6, 2025

All commit authors signed the Contributor License Agreement.

CLA signed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants