python / python/cpython

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

Open
#135,583 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

stdlib type-bug
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reproducer in the issue, focusing on unittest.mock.patch.object, multiprocessing.Pool, and the forkserver start method. Compare its fork and forkserver behavior, then review linked PR gh-152881; done means the reported mock behavior is addressed for forkserver without regressing fork.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
operating-systems
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.