python / python/cpython

dbm.sqlite breaks multi-threaded shelve usage

Đang mở
#131,918 2 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

3.13 3.14 3.15 stdlib topic-sqlite3 type-bug
Ngôn ngữ chính
Python
Star
77.2k
Fork
35.9k
Chỉ số merge pull request
Chỉ số pull request đang chờ

Mô tả

Bug report

Bug description:

dbm backends were previously thread safe, but I think dbm.sqlite introduced in https://github.com/python/cpython/pull/114481 is not.

I am not sure if the resolution should be to add a doc comment to shelve/dbm or some other way to fix it, say specifying a preferred backend or multithreading argument in shelve.open as an argument. (Or if there is a way to fix this in dbm.sqlite itself)

Example code:

from concurrent.futures import ThreadPoolExecutor
import shelve

CACHE = shelve.open('test')

def check_set_cache(value):
    if 'value' in CACHE:
        print(CACHE['value'])
    CACHE['value'] = value
    return CACHE['value']

jobs = list(range(1, 100))
executor = ThreadPoolExecutor(max_workers=5)
entries = list(executor.map(check_set_cache, jobs))
print(entries)

Log

Traceback (most recent call last):
  File "/usr/lib64/python3.13/dbm/sqlite3.py", line 79, in _execute
    return closing(self._cx.execute(*args, **kwargs))
                   ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
sqlite3.ProgrammingError: SQLite objects created in a thread can only be used in that same thread. The object was created in thread id 140036133836608 and this is thread id 140035884230336.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/megh/pybug/reproduce.py", line 14, in <module>
    entries = list(executor.map(check_set_cache, jobs))
  File "/usr/lib64/python3.13/concurrent/futures/_base.py", line 619, in result_iterator
    yield _result_or_cancel(fs.pop())
          ~~~~~~~~~~~~~~~~~^^^^^^^^^^
  File "/usr/lib64/python3.13/concurrent/futures/_base.py", line 317, in _result_or_cancel
    return fut.result(timeout)
           ~~~~~~~~~~^^^^^^^^^
  File "/usr/lib64/python3.13/concurrent/futures/_base.py", line 449, in result
    return self.__get_result()
           ~~~~~~~~~~~~~~~~~^^
  File "/usr/lib64/python3.13/concurrent/futures/_base.py", line 401, in __get_result
    raise self._exception
  File "/usr/lib64/python3.13/concurrent/futures/thread.py", line 59, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/home/megh/pybug/reproduce.py", line 7, in check_set_cache
    if 'value' in CACHE:
       ^^^^^^^^^^^^^^^^
  File "/usr/lib64/python3.13/shelve.py", line 102, in __contains__
    return key.encode(self.keyencoding) in self.dict
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen _collections_abc>", line 817, in __contains__
  File "/usr/lib64/python3.13/dbm/sqlite3.py", line 89, in __getitem__
    with self._execute(LOOKUP_KEY, (key,)) as cu:
         ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib64/python3.13/dbm/sqlite3.py", line 81, in _execute
    raise error(str(exc))
dbm.sqlite3.error: SQLite objects created in a thread can only be used in that same thread. The object was created in thread id 140036133836608 and this is thread id 140035884230336.

(Observed here https://github.com/beancount/beanprice/issues/91 )

CPython versions tested on:

3.13

Operating systems tested on:

Linux

Linked PRs
  • gh-131920

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu với Lib/dbm/sqlite3.py, đặc biệt là _execute, và Lib/shelve.py để hiểu backend được chọn và truy cập như thế nào. Tái hiện ví dụ sử dụng thread trên Python 3.13, sau đó xem lại PR được liên kết gh-131920 và các bình luận hiện có trước khi quyết định hành vi và các bài kiểm thử nào sẽ xác định một bản sửa lỗi.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
python, sqlite
Lĩnh vực
databases
Loại issue
Lỗi
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Cần làm rõ
Mức phù hợp với người mới
20/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.