Skip to content

Memory leak in dict with shared keys #75498

Closed

Description

@vstinner
BPO 31317
Nosy @rhettinger, @pitrou, @vstinner, @methane, @serhiy-storchaka, @zhangyangyu
Superseder
  • bpo-31217: test_code leaked [1, 1, 1] memory blocks on x86 Gentoo Refleaks 3.6/3.x
  • Files
  • test_leak.py
  • 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 = <Date 2017-08-31.22:23:00.056>
    created_at = <Date 2017-08-31.20:18:11.860>
    labels = ['3.7', 'performance']
    title = 'Memory leak in dict with shared keys'
    updated_at = <Date 2017-08-31.22:23:00.054>
    user = 'https://github.com/vstinner'

    bugs.python.org fields:

    activity = <Date 2017-08-31.22:23:00.054>
    actor = 'vstinner'
    assignee = 'none'
    closed = True
    closed_date = <Date 2017-08-31.22:23:00.056>
    closer = 'vstinner'
    components = []
    creation = <Date 2017-08-31.20:18:11.860>
    creator = 'vstinner'
    dependencies = []
    files = ['47111']
    hgrepos = []
    issue_num = 31317
    keywords = []
    message_count = 8.0
    messages = ['301058', '301060', '301062', '301063', '301064', '301065', '301067', '301068']
    nosy_count = 6.0
    nosy_names = ['rhettinger', 'pitrou', 'vstinner', 'methane', 'serhiy.storchaka', 'xiang.zhang']
    pr_nums = []
    priority = 'normal'
    resolution = 'duplicate'
    stage = 'resolved'
    status = 'closed'
    superseder = '31217'
    type = 'resource usage'
    url = 'https://bugs.python.org/issue31317'
    versions = ['Python 3.7']

    Activity

    1. vstinner commented on Aug 31, 2017

      @vstinner
      MemberAuthor

      Copy attached test_leak.py to Lib/test/ and run:

      haypo@selma$ ./python -m test -R 3:30 test_leak

      Run tests in parallel using 1 child processes
      0:00:00 load avg: 0.67 [1/1/1] test_leak failed
      beginning 33 repetitions
      123456789012345678901234567890123
      .................................
      test_leak leaked [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1] memory blocks, sum=30

      1 test failed:
      test_leak

      Total duration: 682 ms
      Tests result: FAILURE

      It seems like Thread2 constructor creates a dictionary on "self.hashvalue = (...)". It's a dictionary sharing keys, but it seems like the dict table memory is not released whne the bug occurs.

      Sadly, I'm only able to reproduce the bug on a 32-bit build. Since the bug depends on exact hash values, maybe the bug exists in 64-bit builds, but is less likely?

      I'm not 100% sure if the bug is related to dict or set.

      The bug comes from bpo-31217.

    2. vstinner commented on Aug 31, 2017

      @vstinner
      MemberAuthor

      If you cannot reproduce the bug on 32-bit build, try to set "FIXED_HASH = 0" in test_leak.py, run "./python -m test -R 3:10 test_leak" multiple times until it fails, then copy the last value of the printed list into hash_values in test_leak.py.

    3. vstinner commented on Aug 31, 2017

      @vstinner
      MemberAuthor

      I spent a lot of time in gdb, but I'm not sure anymore that the issue is related to dict shared keys.

    4. pitrou commented on Aug 31, 2017

      @pitrou
      Member

      It's a really weird bug you found. It depends on the exact values passed to -R:

      $ ./python -m test -R 3:6 test_haypoleak
      Run tests sequentially
      0:00:00 load avg: 0.39 [1/1] test_haypoleak
      beginning 9 repetitions
      123456789
      .........
      test_haypoleak leaked [1, 1, 1, 1, 1, 1] memory blocks, sum=6
      test_haypoleak failed
      
      $ ./python -m test -R 2:6 test_haypoleak
      Run tests sequentially
      0:00:00 load avg: 0.36 [1/1] test_haypoleak
      beginning 8 repetitions
      12345678
      ........
      1 test OK.
    5. pitrou commented on Aug 31, 2017

      @pitrou
      Member

      Actually, it's not the bug that is weird, but the heuristics in -R seem completely weird now. If I print the alloc and rc deltas:

      $ ./python -m test -R 3:6 test_haypoleak
      Run tests sequentially
      0:00:00 load avg: 0.09 [1/1] test_haypoleak
      beginning 9 repetitions
      123456789
      .........
      alloc: [0, 0, 0, 1, 1, 1, 1, 1, 1], rc: [0, 0, 0, 0, 0, 0, 0, 0, 0]
      test_haypoleak leaked [1, 1, 1, 1, 1, 1] memory blocks, sum=6
      test_haypoleak failed
      
      $ ./python -m test -R 2:6 test_haypoleak
      Run tests sequentially
      0:00:00 load avg: 0.10 [1/1] test_haypoleak
      beginning 8 repetitions
      12345678
      ........
      alloc: [0, 0, 0, 1, 1, 1, 1, 1], rc: [0, 0, 0, 0, 0, 0, 0, 0]
      1 test OK.
    6. pitrou commented on Aug 31, 2017

      @pitrou
      Member

      s/completely weird/broken/

      $ ./python -m test -R 2:20 test_haypoleak
      Run tests sequentially
      0:00:00 load avg: 0.10 [1/1] test_haypoleak
      beginning 22 repetitions
      1234567890123456789012
      ......................
      alloc: [0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], rc: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
      1 test OK.
    7. pitrou commented on Aug 31, 2017

      @pitrou
      Member

      To come back to this bug, it still fails if add a __slots__, so I don't think it's related to shared key dictionaries.

    8. vstinner commented on Aug 31, 2017

      @vstinner
      MemberAuthor

      Antoine: "Actually, it's not the bug that is weird, but the heuristics in -R seem completely weird now. If I print the alloc and rc deltas:"

      I changed it recently to reduce false alarms: see bpo-30776.

      Antoine: "To come back to this bug, it still fails if add a __slots__, so I don't think it's related to shared key dictionaries."

      Oh ok, in that case, I will move back to bpo-31217. I close this issue as a duplicate.

    9. transferred this issue fromon Apr 10, 2022
    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.7 (EOL)end of lifeperformancePerformance or resource usage

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions