Skip to content

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

Open
@luk1337

Description

@luk1337

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or error

    Projects

    Status

    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions