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 摘要。