Title: gh-90549: Fix leak of global named resources using multiprocessing spawn by jxdabc · Pull Request #30617 · python/cpython · GitHub
Open Graph Title: gh-90549: Fix leak of global named resources using multiprocessing spawn by jxdabc · Pull Request #30617 · python/cpython
X Title: gh-90549: Fix leak of global named resources using multiprocessing spawn by jxdabc · Pull Request #30617 · python/cpython
Description: 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:repo&can=5 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. ) 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 spawn context, 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 spawn context. 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. https://bugs.python.org/issue46391
Open Graph Description: 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.chrom...
X Description: 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.chrom...
Opengraph URL: https://github.com/python/cpython/pull/30617
X: @github
Domain: github.com
| route-pattern | /:user_id/:repository/pull/:id/checks(.:format) |
| route-controller | pull_requests |
| route-action | checks |
| fetch-nonce | v2:ddc05689-6e42-47d1-e683-dabaceaaa504 |
| current-catalog-service-hash | 87dc3bc62d9b466312751bfd5f889726f4f1337bdff4e8be7da7c93d6c00a25a |
| request-id | 9B10:2C2C4D:272C5BC:352AF11:696AD7AE |
| html-safe-nonce | e37fe3336a3d3a67802a80eb69f44ad69bfdefaa36ef91c85ed8db933740d303 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5QjEwOjJDMkM0RDoyNzJDNUJDOjM1MkFGMTE6Njk2QUQ3QUUiLCJ2aXNpdG9yX2lkIjoiMTM4MjMyNDU4MzA1NzI1MDIyMiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | f8b7733f5a2d6bcc449add0488944bda3117c944f61df3cdb00de0987b817dc1 |
| hovercard-subject-tag | pull_request:823715733 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,checks,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/python/cpython/pull/30617/checks |
| twitter:image | https://avatars.githubusercontent.com/u/2919303?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/2919303?s=400&v=4 |
| og:image:alt | 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.chrom... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 5f99f7c1d70f01da5b93e5ca90303359738944d8ab470e396496262c66e60b8d |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive full-width full-width-p-0 |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 524a93f2c1f36522a3b4be4c110467ee4172245d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width