python / python/cpython

Inconsistent KeyboardInterrupt Handling in multiprocessing due to Context Manager Order

未關閉
#119,104 1 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

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

描述

Bug report

Bug description:

When using multiprocessing.Manager with concurrent.futures.ProcessPoolExecutor, there is a particular ordering of the context managers that results in multiprocessing.managers.py hanging in the method serve_forever, forever. This can be triggered by raising a KeyboardInterrupt while the child process(es), here something, are busy. The ordering that leads to the bug is: Manager first, then ProcessPoolExecutor inside.

import multiprocessing
from concurrent.futures import ProcessPoolExecutor
from time import sleep


def something():
    sleep(10)

# Uncomment one of the following blocks

### Works correctly:

# # Interrupt with CTRL+C while `something` is busy
# # Takes just one KeyboardInterrupts to terminate fully
# with ProcessPoolExecutor() as executor:
#     futures = []
#     with multiprocessing.Manager() as manager:
#         futures.append(executor.submit(something))
#
#         for f in futures:
#             f.result()

### Doesn't work correctly, will hang often, try it a few times:

# # Interrupt with CTRL+C while `something` is busy
# # Takes one KeyboardInterrupts to get stuck, and another to terminate fully
# with multiprocessing.Manager() as manager:
#     with ProcessPoolExecutor() as executor:
#         futures = [executor.submit(something)]
# 
#         for f in futures:
#             f.result()

I am not too familiar with the exact inner workings of these two context managers, but as a user, there was at least nothing to make me aware that the 2nd example is bad. If it's not a bug, and just incorrect ordering, perhaps ProcessPoolExecutor could raise an exception or print a warning that it shouldn't be used inside a Manager context in such a way.

CPython versions tested on:

3.9, 3.10, 3.11, 3.12

Operating systems tested on:

Linux

貢獻指南

開啟貢獻指南

從這裡開始

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

研究方向

首先,使用提供的 ProcessPoolExecutor 和 multiprocessing.Manager 範例重現兩種內容管理器順序,然後檢查 multiprocessing.managers.py 中的 serve_forever 以及相關的關閉路徑。完成的標準是:有問題的順序在 KeyboardInterrupt 後不再掛起,且兩種順序都能一致地終止;issue 中沒有指定測試檔案。

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

評估

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

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

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