python / python/cpython

On Windows, creating a multiprocessing pool leaks handles

未關閉
#154,208 5 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

OS-windows stdlib topic-multiprocessing type-bug
主要語言
Python
星號
77.2k
分支
35.9k
PR 合併指標
PR 指標待擷取

描述

Example:

import ctypes.wintypes
import gc
import multiprocessing.pool

kernel32 = ctypes.WinDLL('kernel32', use_last_error=True)

kernel32.GetCurrentProcess.argtypes = ()
kernel32.GetCurrentProcess.restype = ctypes.wintypes.HANDLE

kernel32.GetProcessHandleCount.argtypes = (ctypes.wintypes.HANDLE, ctypes.wintypes.LPDWORD)
kernel32.GetProcessHandleCount.restype = ctypes.wintypes.BOOL

def handle_count():
    # Pseudo-handle that doesn't need to be closed
    hproc = kernel32.GetCurrentProcess()

    handle_count = ctypes.wintypes.DWORD()
    if not kernel32.GetProcessHandleCount(hproc, ctypes.byref(handle_count)):
        raise ctypes.WinError(ctypes.get_last_error())

    return handle_count.value

def func():
    pool = multiprocessing.pool.Pool(1)
    pool.terminate()
    pool.join()
    pool.close()
    pool = None
    gc.collect()

def main():
    print("Initial handle count:", handle_count())
    for _ in range(4):
        func()
        print("Handle count:", handle_count())

if __name__ == "__main__":
    main()

Output with Python 3.16 on Windows:

Initial handle count: 116
Handle count: 123
Handle count: 127
Handle count: 131
Handle count: 135

I discovered the issue while working on PR gh-154140 which detects leaks of Windows handles in the Python test suite.

Linked PRs
  • gh-154365

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

先在 Windows 上重現 main() 和 func(),然後追蹤 multiprocessing.pool.Pool 的建立與終止,以確認哪些控制代碼仍保持開啟。檢視連結的 gh-154365,以及 gh-154140 中控制代碼洩漏的背景。完成的條件是重複建立集區不再增加回報的處理程序控制代碼數量。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
python
領域
operating-systems
Issue 類型
缺陷
難度
4/5
預估耗時
3-5 天
活躍度
停滯
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。