python / python/cpython

dbm.sqlite breaks multi-threaded shelve usage

未关闭
#131,918 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

3.13 3.14 3.15 stdlib topic-sqlite3 type-bug
主要语言
Python
星标
77.2k
派生
35.9k
PR 合并指标
PR 指标待抓取

描述

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

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从 Lib/dbm/sqlite3.py 开始,尤其关注 _execute,并查看 Lib/shelve.py,以了解后端是如何选择和访问的。在 Python 3.13 上复现线程示例,然后查看链接的 PR gh-131920 和现有评论,再决定哪些行为和测试可以定义一个修复方案。

由索引模型根据 Issue 内容生成。

评估

技术栈
python, sqlite
领域
databases
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
需要澄清
新手友好度
20/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。