Skip to content

iterators broken for weak dicts #39529

Closed
Closed

Description

@dcjim
mannequin
BPO 839159
Nosy @mdickinson, @pitrou, @devdanzin
Superseder
  • bpo-7105: weak dict iterators are fragile because of unpredictable GC runs
  • Files
  • weakrefbug.py: Script that demonstrates the bug
  • weakref_dict_iter.diff
  • 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 2010-07-23.09:56:04.895>
    created_at = <Date 2003-11-10.11:32:12.000>
    labels = ['type-bug', 'library']
    title = 'iterators broken for weak dicts'
    updated_at = <Date 2010-07-23.09:56:04.894>
    user = 'https://bugs.python.org/dcjim'

    bugs.python.org fields:

    activity = <Date 2010-07-23.09:56:04.894>
    actor = 'mark.dickinson'
    assignee = 'none'
    closed = True
    closed_date = <Date 2010-07-23.09:56:04.895>
    closer = 'mark.dickinson'
    components = ['Library (Lib)']
    creation = <Date 2003-11-10.11:32:12.000>
    creator = 'dcjim'
    dependencies = []
    files = ['1091', '9537']
    hgrepos = []
    issue_num = 839159
    keywords = ['patch']
    message_count = 14.0
    messages = ['18959', '62908', '82020', '82037', '82062', '82066', '82067', '104842', '105159', '110338', '111254', '111256', '111257', '111258']
    nosy_count = 8.0
    nosy_names = ['dcjim', 'tseaver', 'mark.dickinson', 'pitrou', 'ajaksu2', 'vdupras', 'elachuni', 'BreamoreBoy']
    pr_nums = []
    priority = 'normal'
    resolution = 'duplicate'
    stage = 'patch review'
    status = 'closed'
    superseder = '7105'
    type = 'behavior'
    url = 'https://bugs.python.org/issue839159'
    versions = ['Python 2.6', 'Python 2.7']

    Activity

    1. dcjim commented on Nov 10, 2003

      dcjimmannequin
      MannequinAuthor

      You can't use iterators on wekref dicts because items
      might be removed from the dictionaries while iterating
      due to GC.

      I've attached a script that illustrates the bug with
      Python 2.3.2. It doesn't matter whether you use weak
      key or weak value dicts.

      If this can't be fixed, then the iteration methods should
      either be removed or made to (lamely) create intermediate
      lists to work around the problem.

    2. added
      stdlib
      on Nov 10, 2003
    3. changed the title [-]interators broken for weak dicts[/-] [+]iterators broken for weak dicts[/+] on Feb 22, 2008
    4. vdupras commented on Feb 24, 2008

      vduprasmannequin
      Mannequin

      I made a patch to fix the problem. The cleaning up of they weakref keys or
      values will be held until all references to iterators created by the
      weakdict are dead.

      I also couldn't resist removing code duplication of code in items(),
      keys() and values().

      At first, I couldn't understand why this whole remove(), _remove() and
      selfref() mechanism was in place. I had removed them and replaced them
      with methods, and the tests still passed. Then I realized it was to make
      sure keys and values didn't prevent the weak dicts from being freed. I
      added tests for this.

    5. added
      type-bug
      on Feb 24, 2008
    6. devdanzin commented on Feb 14, 2009

      devdanzinmannequin
      Mannequin

      Patch has tests, may need updating.

    7. pitrou commented on Feb 14, 2009

      @pitrou
      Member

      Interesting patch. I think the intermediate assertEquals in
      test_weak_*_dict_flushed_dead_items_when_iters_go_out are just testing
      an implementation detail, only the final one should remain.

      Also, it is likely the "code duplication" you are talking about was
      there for performance reasons, so I'd suggest putting it back in.

    8. vdupras commented on Feb 14, 2009

      vduprasmannequin
      Mannequin

      About duplicated code and performance:

      When I look at the duplicated code, I don't see anything that remotely
      looks like a performance tweak. Just to make sure, I made a bench:

      #!/usr/bin/env python
      import sys
      sys.path.insert(0, 'Lib')
      import timeit
      import weakref
      
      class Foo(object): pass
      def setup():
          L = [Foo() for i in range(1000)]
          global d
          d = weakref.WeakValueDictionary(enumerate(L))
          del L[:500] # have some dead weakrefs
      
      def do():
          d.values()

      print timeit.timeit(do, setup, number=100000)

      Results without the patch:
      ./python.exe weakref_bench.py
      0.804216861725

      Results with the patch:
      $ ./python.exe weakref_bench.py
      0.813000202179

      I think the small difference in performance is more attributable to the
      extra processing the weakref dict does than the deduplication of the
      code itself.

      About the test_weak_*_dict_flushed_dead_items_when_iters_go_out:

      If a weakref dict keeps its weak reference alive, it's not an
      implementation detail, it's a bug. The whole point of using such dicts
      is to not keep keys or values alive when they go out.

    9. vdupras commented on Feb 14, 2009

      vduprasmannequin
      Mannequin

      Oh, that's me again not correctly reading my own tests. It's the
      *are_not_held* tests that test that no reference is kept.
      I agree about the *flushed_dead_items* being an implementation detail.

    10. pitrou commented on Feb 14, 2009

      @pitrou
      Member

      Results without the patch:
      ./python.exe weakref_bench.py
      0.804216861725

      Results with the patch:
      $ ./python.exe weakref_bench.py
      0.813000202179

      Thanks for the numbers, I see my worries were unfounded.

      About the test_weak_*_dict_flushed_dead_items_when_iters_go_out:

      If a weakref dict keeps its weak reference alive, it's not an
      implementation detail, it's a bug. The whole point of using such dicts
      is to not keep keys or values alive when they go out.

      I was talking about doing self.assertEqual(len(d), self.COUNT) before
      deleting the iterators.

    11. tseaver commented on May 3, 2010

      @tseaver

      I can confirm that the patch applies with minimal fuzz to the
      release26-maint branches and the trunk, and that the added tests fail
      without the updated implementation in both cases.

      Furthermore, Jim's original demo script emits it error with my stock 2.6.5
      Python, but is silent with the patched trunk / 2.6 branch.

    12. elachuni commented on May 6, 2010

      elachunimannequin
      Mannequin

      Probably old news, but this also affects 2.5.4.

    13. BreamoreBoy commented on Jul 14, 2010

      BreamoreBoymannequin
      Mannequin

      If this is to go forward the patch will need porting to 2.7, 3.1 and 3.2

    14. vdupras commented on Jul 23, 2010

      vduprasmannequin
      Mannequin

      It looks like this issue has been fixed in bpo-7105 already. Can we close this ticket?

    15. mdickinson commented on Jul 23, 2010

      @mdickinson
      Member

      It's not yet fixed in 2.7 or 2.6. Updating versions.

    16. vdupras commented on Jul 23, 2010

      vduprasmannequin
      Mannequin

      We might as well backport Antoine's patch rather than take this one (even if mine for 2.x already). It would be weird to have 2 wildly different patches to solve the same problem.

      Maybe close this ticket and flag bpo-7105 for backporting?

    17. mdickinson commented on Jul 23, 2010

      @mdickinson
      Member

      Agreed.

    18. transferred this issue fromon Apr 9, 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

      stdlibtype-bug

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions