Skip to content

gh-79805: Clarify thread-safety limitations in shelve module #137997

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
12 changes: 9 additions & 3 deletions Doc/library/shelve.rst
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,17 @@
database should be fairly small, and in rare cases key collisions may cause
the database to refuse updates.

* The :mod:`shelve` module does not support *concurrent* read/write access to

Check warning on line 136 in Doc/library/shelve.rst

View workflow job for this annotation

GitHub Actions / Docs / Docs

py:attr reference target not found: writeback [ref.attr]
shelved objects. (Multiple simultaneous read accesses are safe.) When a
shelved objects, whether from different programs or different threads within
the same program. (Multiple simultaneous read accesses are safe.) When a
program has a shelf open for writing, no other program should have it open for
reading or writing. Unix file locking can be used to solve this, but this
differs across Unix versions and requires knowledge about the database
reading or writing. Additionally, within a single program, concurrent access
from multiple threads is not supported and may lead to data corruption,
unexpected behavior, or crashes. This affects all writing operations including
Copy link
Member

Choose a reason for hiding this comment

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

Out of the loop here, but at a glance, is there any way to prevent the crashes (e.g. with some simple locking)? We typically don't want pure-Python code to be able to crash, unless it's in ctypes.

assignment, deletion, :meth:`~Shelf.sync`, and :meth:`~Shelf.reorganize`.
Even with :attr:`writeback` set to :const:`True`, thread-safety is not
guaranteed. Unix file locking can be used to solve cross-program concurrency,
but this differs across Unix versions and requires knowledge about the database
implementation used.

* On macOS :mod:`dbm.ndbm` can silently corrupt the database file on updates,
Expand Down
Loading