GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Library multiprocess leaks named resources. · Issue #90549 · python/cpython

Library multiprocess leaks named resources. #90549

New issue
Copy link
New issue
Copy link
Closed
Closed
Library multiprocess leaks named resources.#90549
Copy link
Labels
3.10 (EOL)end of lifeend of life3.11only security fixesonly security fixes3.8 (EOL)end of lifeend of life3.9 (EOL)end of lifeend of lifeperformancePerformance or resource usagePerformance or resource usagestdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytopic-multiprocessing

Description

jxdabc
mannequin
opened on Jan 15, 2022
Issue body actions
BPO 46391
Nosy @pitrou, @benjaminp, @1st1, @applio, @arhadthedev, @jxdabc, @BarkingBad
PRs
  • gh-90549: Fix leak of global named resources using multiprocessing spawn #30617
  • Files
  • screen.png: screenshot
  • 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:

    assignee = None
    closed_at = None
    created_at = <Date 2022-01-15.18:04:57.278>
    labels = ['3.8', '3.9', '3.10', 'performance', '3.11', 'library']
    title = 'Library multiprocess leaks named resources.'
    updated_at = <Date 2022-02-18.19:39:09.294>
    user = 'https://github.com/jxdabc'

    bugs.python.org fields:

    activity = <Date 2022-02-18.19:39:09.294>
    actor = 'BarkingBad'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Library (Lib)']
    creation = <Date 2022-01-15.18:04:57.278>
    creator = 'milestonejxd'
    dependencies = []
    files = ['50580']
    hgrepos = []
    issue_num = 46391
    keywords = ['patch']
    message_count = 5.0
    messages = ['410654', '410657', '410853', '411456', '413504']
    nosy_count = 8.0
    nosy_names = ['pitrou', 'benjamin.peterson', 'sbt', 'yselivanov', 'davin', 'arhadthedev', 'milestonejxd', 'BarkingBad']
    pr_nums = ['30617']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'resource usage'
    url = 'https://bugs.python.org/issue46391'
    versions = ['Python 3.8', 'Python 3.9', 'Python 3.10', 'Python 3.11']

    Activity

    Next
    1. jxdabc commented on Jan 15, 2022

      @jxdabc
      jxdabcmannequin
      on Jan 15, 2022
      MannequinAuthor
      More actions

      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=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.

      1. The main module is reloaded in the subprocess (for pickle) in multiprocessing::spawn::_main.
      2. Named resources (such as the semaphore above) in the main module resister their _cleanup into multiprocessing::util::_finalizer_registry, which unlink themselves.
      3. 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.

    2. jxdabc
      added
      3.9 (EOL)end of lifeend of life
      stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
      performancePerformance or resource usagePerformance or resource usage
      on Jan 15, 2022
    3. arhadthedev commented on Jan 15, 2022

      @arhadthedev
      arhadthedev
      on Jan 15, 2022
      Member
      More actions

      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/\>).

    4. jxdabc commented on Jan 18, 2022

      @jxdabc
      jxdabcmannequin
      on Jan 18, 2022
      MannequinAuthor
      More actions

      loop file owner.

    5. jxdabc commented on Jan 24, 2022

      @jxdabc
      jxdabcmannequin
      on Jan 24, 2022
      MannequinAuthor
      More actions

      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/[email protected]/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/[email protected]/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/[email protected]/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 '

    6. jxdabc
      added
      3.8 (EOL)end of lifeend of life
      3.10 (EOL)end of lifeend of life
      3.11only security fixesonly security fixes
      on Jan 24, 2022
    7. 16 remaining items

    8. pitrou
      added a commit that references this issue on Jun 9, 2022

      gh-90549: Fix leak of global named resources using multiprocessing sp…

      30610d2
    9. miss-islington
      added 4 commits that reference this issue on Jun 9, 2022

      pythongh-90549: Fix leak of global named resources using multiprocess…

      54e06c1

      pythongh-90549: Fix leak of global named resources using multiprocess…

      fa9a91a

      gh-90549: Fix leak of global named resources using multiprocessing sp…

      2ad51c6

      gh-90549: Fix leak of global named resources using multiprocessing sp…

      aee7d3d
    10. pitrou commented on Jun 10, 2022

      @pitrou
      pitrou
      on Jun 10, 2022
      Member
      More actions

      Okay, this is fixed now. Thanks @jxdabc for the contribution!

    11. pitrou
      closed this as completedon Jun 10, 2022
    12. serhiy-storchaka
      added a commit that references this issue on Jun 26, 2022

      Revert 30935 defer bpo45162 to 312 (#94285)

      1ffde3e
    13. arhadthedev
      moved this from Todo to Done in Contribution Queueon Feb 6, 2023
    14. dturner commented on Aug 17, 2023

      @dturner
      dturner
      on Aug 17, 2023
      More actions

      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/[email protected]/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 kosiek
    15. siennathesane commented on Sep 11, 2023

      @siennathesane
      siennathesane
      on Sep 11, 2023
      More actions

      This is also happening in 3.11.5

      /opt/homebrew/Cellar/[email protected]/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 kosiek
    16. 3DMiller commented on Mar 4, 2024

      @3DMiller
      3DMiller
      on Mar 4, 2024
      More actions

      Has this been resolved? Issue on macOS running python 3.9.12.

    17. gpshead commented on Mar 4, 2024

      @gpshead
      gpshead
      on Mar 4, 2024
      Member
      More actions

      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.)

    18. tumma72
      added a commit that references this issue on Mar 5, 2026

      fix: clean shutdown — dispose database engine and suppress resource_t…

      8c52e6f
    19. fbattaglin
      added a commit that references this issue on May 3, 2026

      fix: suppress benign multiprocessing semaphore warning on shutdown

      9399963
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      3.10 (EOL)end of lifeend of life3.11only security fixesonly security fixes3.8 (EOL)end of lifeend of life3.9 (EOL)end of lifeend of lifeperformancePerformance or resource usagePerformance or resource usagestdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytopic-multiprocessing

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions

        You are visitor 01402503 — GeezerVison does not store the pages you look up.
        Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.