python / python/cpython

`OSError` when creating a `multiprocessing.Queue` with `maxsize > 32767`

未关闭
#119,534 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

stdlib type-bug
主要语言
Python
星标
77.2k
派生
36k
PR 合并指标
PR 指标待抓取

描述

Bug report

Bug description:

While exploring multiprocessing.Queue, I encountered an exception which was confusing and I couldn't reconcile from Googling. Briefly, it seems that the Queue object allows providing a maxsize which exceeds from .synchronize import SEM_VALUE_MAX. If that occurs, the exception below is raised.

I suspect but haven't confirmed that this issue exists in main as the maxsize does not carry an upper bound.

If this is acknowledged as a bug, or at least a source of improvement for the user experience, I would be happy to provide a pull request that (1) sets an upper bound as SEM_VALUE_MAX and (2) provides a Warning that the upper bound was exceeded.

$ cat queue-test.py 
import multiprocessing as mp
import sys

class mw:
    def __init__(self, q):
        self.q = q

    def writer(self):
        for i in range(5):
            self.q.put(i)
            print(f"writer placed {i}", flush=True)
        self.q.put(None)

    def reader(self):
        while True:
            i = self.q.get()
            if i is None:
                break
            print(f"reader got {i}", flush=True)

if __name__ == '__main__':
    maxsize = int(sys.argv[1])
    ctx = mp.get_context('spawn')
    qq = ctx.Queue(maxsize)

    f = mw(qq)
    wp = ctx.Process(target=f.writer)
    wp.start()
    rp = ctx.Process(target=f.reader)
    rp.start()
    wp.join()
    rp.join()
$ python queue-test.py 32767
writer placed 0
writer placed 1
writer placed 2
writer placed 3
writer placed 4
reader got 0
reader got 1
reader got 2
reader got 3
reader got 4
$ python queue-test.py 32768
Traceback (most recent call last):
  File "/Users/dtmcdonald/queue-test.py", line 24, in <module>
    qq = ctx.Queue(maxsize)
         ^^^^^^^^^^^^^^^^^^
  File "/Users/dtmcdonald/miniconda3/envs/duckdb/lib/python3.11/multiprocessing/context.py", line 103, in Queue
    return Queue(maxsize, ctx=self.get_context())
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/dtmcdonald/miniconda3/envs/duckdb/lib/python3.11/multiprocessing/queues.py", line 49, in __init__
    self._sem = ctx.BoundedSemaphore(maxsize)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/dtmcdonald/miniconda3/envs/duckdb/lib/python3.11/multiprocessing/context.py", line 88, in BoundedSemaphore
    return BoundedSemaphore(value, ctx=self.get_context())
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/dtmcdonald/miniconda3/envs/duckdb/lib/python3.11/multiprocessing/synchronize.py", line 152, in __init__
    SemLock.__init__(self, SEMAPHORE, value, value, ctx=ctx)
  File "/Users/dtmcdonald/miniconda3/envs/duckdb/lib/python3.11/multiprocessing/synchronize.py", line 57, in __init__
    sl = self._semlock = _multiprocessing.SemLock(
                         ^^^^^^^^^^^^^^^^^^^^^^^^^
OSError: [Errno 22] Invalid argument
$ python -c "from multiprocessing.synchronize import SEM_VALUE_MAX; print(SEM_VALUE_MAX)"
32767
CPython versions tested on:

3.11, 3.12

Operating systems tested on:

macOS

Linked PRs
  • gh-119542

贡献指南

打开贡献指南

从这里开始

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

调研方向

先从链接的 gh-119542 开始,然后检查 Lib/multiprocessing/queues.py 和 Lib/multiprocessing/synchronize.py 中与 Queue 初始化和 SEM_VALUE_MAX 相关的部分。使用 maxsize 值 32767 和 32768 通过 queue-test.py 重现该行为;done 应处理超过受支持 semaphore 上限的值所导致的无效参数失败。

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

评估

技术栈
python
领域
operating-systems
Issue 类型
缺陷
难度
3/5
预计耗时
1-2 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
25/100

把新 issue 发到你的邮箱

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