python / python/cpython

multiprocessing.Queue.empty() returns False after all producers have exited on Windows 3.13.3

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

还没有人认领这个 Issue。

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

描述

On Windows 11 with CPython 3.13.3 official installer, Queue.empty() incorrectly returns False when the producer process has already terminated, allowing a later-started consumer to successfully get() all buffered items. This contradicts the documented behaviour that empty() is only reliable while producers are alive.
To Reproduce
Save as clean_test.py:
Python

from multiprocessing import Process, Queue

def x(q):
    for i in range(4):
        q.put(i)

def y(q):
    cnt = 0
    while True:
        if q.empty():
            break
        q.get()
        cnt += 1
    print('实际 get 次数:', cnt)

if __name__ == '__main__':
    q = Queue()
    p1 = Process(target=x, args=(q,))
    p2 = Process(target=y, args=(q,))
    p1.start()
    p1.join()          # producer already dead
    p2.start()
    p2.join()

Run:
powershell

python clean_test.py
Expected behaviour
Should print 实际 get 次数: 0 (consumer sees empty queue).
Actual behaviour
Prints 实际 get 次数: 4 (consumer successfully gets all items).
Environment
OS: Windows 11 (build 22631)
Python: 3.13.3 (tags/v3.13.3:6280bb5, Apr 8 2025, 14:47:33) [MSC v.1943 64 bit (AMD64)]
Installer: official python.org x64 exe
File SHA-256: 4D8746575FF5D9062D159EB0C382589DBFEC56C942D23DA57E44CF1920312D57
Additional context
The same script always outputs 0 on Linux (CPython 3.11-3.13) and on Windows 10 with 3.11.
Appears to be Windows-specific and version-specific (3.13.3).

贡献指南

打开贡献指南

从这里开始

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

调研方向

从复现程序 clean_test.py 开始,在 Windows 11 上使用 CPython 3.13.3 运行它,然后比较 issue 中描述的 Windows 10 和 Linux 结果。调查 multiprocessing.Queue.empty() 以及 producer/consumer 的生命周期;当 Windows 3.13.3 的情况在 producer 退出后报告 0 个项目,且比较环境没有发生回归时,即视为完成。

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

评估

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

把新 issue 发到你的邮箱

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