unittest mocks don't get applied to children with forkserver multiprocessing start method
Open
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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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