python / python/cpython

unittest mocks don't get applied to children with forkserver multiprocessing start method

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

還沒有人認領這個 Issue。

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

描述

Bug report

Bug description:
import multiprocessing
import unittest
from unittest import mock


class Hello(unittest.TestCase):
    def _test_world(x):
        print(x, World().test())

    def test_world(self):
        # OK: multiprocessing.set_start_method("fork")
        # NOK: multiprocessing.set_start_method("forkserver")

        with mock.patch.object(World, "test", return_value=42):
            with multiprocessing.Pool(5) as pool:
                pool.map(Hello._test_world, range(5))


class World:
    def test(self):
        return 1

With "fork":

test.py 0 42
1 42
2 42
3 42
4 42

With "forkserver":

test.py 0 1
1 1
2 1
4 1
3 1
CPython versions tested on:

3.14

Operating systems tested on:

Linux

Linked PRs
  • gh-152881

貢獻指南

開啟貢獻指南

從這裡開始

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

研究方向

從 issue 中的 reproducer 開始,著重於 unittest.mock.patch.object、multiprocessing.Pool 和 forkserver 啟動方法。比較其在 fork 和 forkserver 下的行為,然後檢視連結的 PR gh-152881;完成的標準是,已解決所回報的 mock 行為在 forkserver 下的問題,同時不會導致 fork 回歸。

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

評估

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

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

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