multiprocessing.Pool: leak of global named resources using multiprocessing spawn
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 77.2k
- Forks
- 35.9k
- PR merge metrics
- PR metrics pending
Description
Bug report
Bug description:
I'm receiving a warning when using the multiprocessing.Pool in spawn mode.
/opt/pyenv/versions/3.12.4/lib/python3.12/multiprocessing/resource_tracker.py:254: UserWarning: resource_tracker: There appear to be 1 leaked semaphore objects to clean up at shutdown
warnings.warn('resource_tracker: There appear to be %d '
A similar issue was brought up and fixed before, when multiprocessing.Process was used:
https://github.com/python/cpython/pull/30617
The test case introduced by the PR above: https://github.com/python/cpython/blob/3.12/Lib/test/_test_multiprocessing.py#L6040C7-L6040C24
To trigger the warning, I just replaced the Process with Pool as follows:
import multiprocessing as mp
ctx = mp.get_context('spawn')
global_resource = ctx.Semaphore()
def submain(): pass
if __name__ == '__main__':
p = ctx.Pool(processes=3).starmap(submain, [tuple() for i in range(5)])
CPython versions tested on:
3.11, 3.12, CPython main branch
Operating systems tested on:
Linux
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 multiprocessing resource-tracking behavior shown in the report and compare the existing Process fix in PR 30617 with Pool handling. Use the referenced regression test in Lib/test/_test_multiprocessing.py as the starting point, then reproduce the spawn-mode Pool example on Linux. Done means the example no longer reports a leaked semaphore and the behavior is covered by a regression test.
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
- Mostly clear
- Newbie friendliness
- 35/100