Repository navigation
Library multiprocess leaks named resources. #90549
Description
Activity
Repo is the standard tool that Google uses to build Android, Chrome OS, Chromium, etc, written in Python.
Many Repo users have encountered resource leak warnings with Python 3.9.
https://bugs.chromium.org/p/gerrit/issues/detail?id=14934&q=component%3Arepo&can=5I did some work and found that the problem is not caused by the code of Repo, but a bug of the Python library, multiprocess.
To make it simple, the Python script below leaks named resource even when exiting normally. (And be unlinked by resource_tracker with a warning. )
import multiprocessing as mp global_resource = mp.Semaphore() def submain(): pass if __name__ == '__main__': p = mp.Process(target=submain) p.start() p.join()Tested on macOS with Python 3.9.7
python test.py
resource_tracker: There appear to be 1 leaked semaphore objects to clean up at shutdown.This bug will 100% reproduce when then main module uses named resources as global variables and uses
spawncontext, which is the case of Repo on macOS.This is caused by multiprocess::BaseProcess::_bootstrap.
When a new process is started with multiprocessing.Process.start() in
spawncontext.- The main module is reloaded in the subprocess (for pickle) in multiprocessing::spawn::_main.
- Named resources (such as the semaphore above) in the main module resister their _cleanup into multiprocessing::util::_finalizer_registry, which unlink themselves.
- multiprocess::BaseProcess::_bootstrap then clears _finalizer_registry.
When a subprocess is spawned, it is no need to clear util::_finalizer_registry (and no need to call util::_run_after_forkers). Disable clearing _finalizer_registry (and disable call to _run_after_forkers) should fix this bug without breaking anything else.
And I uploaded a MR.
- added3.9 (EOL)end of lifeend of lifestdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directoryperformancePerformance or resource usagePerformance or resource usage
on Jan 15, 2022 I added core devs related to multiprocessing into a nosy list so they got a notification and the PR will be evaluated and merged faster. FYI, the devs are Davin Potts and Antoine Pitrou (as per <https://devguide.python.org/experts/\>).
loop file owner.
update,
Confirmed to affect all versions above 3.8+.
It's really annoying.
Command line tools like Repo get a warning after every (success) command.
And mess up with shell.-------------------------------------------------------------------
$ repo status . project ksbox/ branch dev $ /usr/local/Cellar/python@3.9/3.9.7/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/resource_tracker.py:216: 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 '
$
$
$
$ repo sync .
Fetching: 100% (1/1), done in 1.184s
Garbage collecting: 100% (1/1), done in 0.013s
repo sync has finished successfully.
$ /usr/local/Cellar/python@3.9/3.9.7/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/resource_tracker.py:216: 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 '$
$
$
$ repo status .
project ksbox/ branch dev
$ /usr/local/Cellar/python@3.9/3.9.7/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/resource_tracker.py:216: 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 '- added3.8 (EOL)end of lifeend of life3.10 (EOL)end of lifeend of life3.11only security fixesonly security fixes
on Jan 24, 2022 16 remaining items
- added a commit that references this issue
on Jun 9, 2022 - added 4 commits that reference this issue
on Jun 9, 2022 Okay, this is fixed now. Thanks @jxdabc for the contribution!
- added a commit that references this issue
on Jun 26, 2022 Which version(s) was this fix applied to? I'm still getting this error in the latest version of Python 3.11.4 when running
repo status/Users/donturner/homebrew/Cellar/python@3.11/3.11.4_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/multiprocessing/resource_tracker.py:224: 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 'Reacted by Pat Patterson, Redoubts, Tyler Ferrara and kosiekThis is also happening in 3.11.5
/opt/homebrew/Cellar/python@3.11/3.11.5/Frameworks/Python.framework/Versions/3.11/lib/python3.11/multiprocessing/resource_tracker.py:224: UserWarning: resource_tracker: There appear to be 6 leaked semaphore objects to clean up at shutdown warnings.warn('resource_tracker: There appear to be %d 'Reacted by Pat Patterson, Redoubts, Li Mu Zhi and kosiekHas this been resolved? Issue on macOS running python 3.9.12.
3.10.6+ and 3.11.0+ contain PR #30617. The reproducer code listed in the opening message does not cause the Resource warning for me (including when explicitly setting the start method to 'spawn'). If you're still seeing this on recent Python versions, we need a simple way to reproduce it.
I recommend opening a new issue when you have one.
(Just because you see this error message and find your way here does not mean that Python itself is the cause -- it can also be an application bug, we currently lack sufficient information to say one way or the other.)
- added a commit that references this issue
on Mar 5, 2026 - added a commit that references this issue
on May 3, 2026
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields: