Skip to content
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

PersistentDict: thread safety #234

Merged
merged 17 commits into from
Jul 2, 2024
Merged

Conversation

matthiasdiener
Copy link
Contributor

@matthiasdiener matthiasdiener commented Jun 10, 2024

Workaround for #233.

cc: illinois-ceesd/logpyle#106 illinois-ceesd/logpyle#107 illinois-ceesd/logpyle#108

Based partly on https://ricardoanderegg.com/posts/python-sqlite-thread-safety/.

Notes:

  • Full thread safety ("serialized mode" in SQLite) is the default mode of SQLite (https://sqlite.org/threadsafe.html)
  • There appears to be no way thread safety can be set from the Python interface

The only other alternative I can see is to use a fully multithreaded implementation such as https://github.com/piskvorky/sqlitedict

@matthiasdiener
Copy link
Contributor Author

This is ready for a first look @inducer @vincefn

pytools/__init__.py Outdated Show resolved Hide resolved
Comment on lines 199 to 203
SQLite-related functions
------------------------

.. autofunction:: get_sqlite3_thread_safety_level
.. autofunction:: is_sqlite3_fully_threadsafe
Copy link
Owner

Choose a reason for hiding this comment

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

I'm not sure I'd like to document these, since it encodes things that feel like implementation details. Maybe push this off into pytools._sqlite?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moving these functions into a separate module makes sense I think, but I thought these functions are pretty generic? Which implementation details do they encode?

Copy link
Owner

Choose a reason for hiding this comment

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

How cpython and threads and sqlite interact. The details of how this works at the moment is kind of insane ATM, and I try to avoid insanity in publicly exposed interfaces. Hence my preference to keep this out of public view.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

How cpython and threads and sqlite interact. The details of how this works at the moment is kind of insane ATM

I see - is this in reference to the translation between thread support levels in 07412b2#diff-2d67e0c2f525bd7f05e4909f1248231d4b1f3b92d2f1be94487d5b2d4cb46b78R2992-R2996, or are there other problems in interacting between threads/sqlite? If so, could those be the issue for the failures I mentioned in #234 (comment)?

pytools/persistent_dict.py Show resolved Hide resolved
@matthiasdiener
Copy link
Contributor Author

Another idea: in case the sqlite library is not built with thread safety, can we just use a mutex every time we use the connection (e.g., in _exec_sql_fn), and also set check_same_thread=False?

@matthiasdiener
Copy link
Contributor Author

matthiasdiener commented Jun 10, 2024

It seems it is crashing in the multithreaded case where the connection is shared among threads, a situation which should not crash even without the mutex :-(

  File "/home/runner/work/pytools/pytools/pytools/persistent_dict.py", line 778, in fetch
    stored_key, value = pickle.loads(row[0])
                        ^^^^^^^^^^^^^^^^^^^^
TypeError: a bytes-like object is required, not 'NoneType'

@matthiasdiener matthiasdiener changed the title PersistentDict: disable same-thread check on threadsafe SQLite PersistentDict: thread safety Jun 12, 2024
@matthiasdiener
Copy link
Contributor Author

matthiasdiener commented Jun 12, 2024

Well, that turned out to be more ... interesting... than expected. The final error only happened with Python 3.12, which made debugging this more difficult.

This is ready for testing/review.

@vincefn: Could you test this PR against the failure you had mentioned in #233?

It does avoid the warning I had seen in the boxtree test in #233.

@matthiasdiener matthiasdiener marked this pull request as ready for review June 12, 2024 22:59
Every sql command needs to be wrapped in _exec_sql, for the
'spin while busy' functionality
@vincefn
Copy link

vincefn commented Jun 17, 2024

Sorry it took a while - I could only reproduce that inside a gitlab CI test... But the tests which failed are now passing with this fix :-)

@inducer inducer enabled auto-merge (squash) July 2, 2024 18:31
@inducer inducer disabled auto-merge July 2, 2024 18:32
@inducer inducer enabled auto-merge (squash) July 2, 2024 18:42
@inducer inducer merged commit a3015f1 into inducer:main Jul 2, 2024
16 checks passed
@matthiasdiener matthiasdiener deleted the pdict-threads branch July 2, 2024 18:47
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.

3 participants