New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Library multiprocess leaks named resources. #90549
Comments
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. I 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. )
Tested on macOS with Python 3.9.7
This bug will 100% reproduce when then main module uses named resources as global variables and uses This is caused by multiprocess::BaseProcess::_bootstrap. When a new process is started with multiprocessing.Process.start() in
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. |
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. ------------------------------------------------------------------- $ 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 ' $ $ |
Also hitting this problem. Would be grateful for fixing it, since there is already PR created |
any news on this issue? |
Strangely, I see this error only when running inside PyCharm in debug mode, and it happens non-deterministically. When I run in cmd line it does not happen. |
This problem is related to import multiprocessing as mp
mp.set_start_method('fork') # switch to fork
global_resource = mp.Semaphore()
def submain():
pass
if __name__ == '__main__':
p = mp.Process(target=submain)
p.start()
p.join() To avoid this warning in another way, create global_resource inside import multiprocessing as mp
def submain():
pass
if __name__ == '__main__':
global_resource = mp.Semaphore()
p = mp.Process(target=submain)
p.start()
p.join() Test environment is macos python 3.9, example above can be a temp solution, hoping MR fix this soon. |
Thanks for your workaround and tracking down. Unfortunately, in the case of Google Repo. The global resource is not at the main module. Instead a module which the main module imports creates a global_resource. So, the workaround won't help in this case. : ( |
It would be nice to have this fixed....this warning appears on most of the repo commands I use :-( |
me, too :-( |
…awn (#30617) Co-authored-by: XD Trol <milestonejxd@gmail.com> Co-authored-by: Antoine Pitrou <pitrou@free.fr>
…ing spawn (pythonGH-30617) Co-authored-by: XD Trol <milestonejxd@gmail.com> Co-authored-by: Antoine Pitrou <pitrou@free.fr> (cherry picked from commit 30610d2) Co-authored-by: Leo Trol <milestone.jxd@gmail.com>
…ing spawn (pythonGH-30617) Co-authored-by: XD Trol <milestonejxd@gmail.com> Co-authored-by: Antoine Pitrou <pitrou@free.fr> (cherry picked from commit 30610d2) Co-authored-by: Leo Trol <milestone.jxd@gmail.com>
Okay, this is fixed now. Thanks @jxdabc for the contribution! |
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:
The text was updated successfully, but these errors were encountered: