#13394: Write a WeakValueDictionary with safer key removal
-------------------------------------+-------------------------------------
       Reporter:  nbruin             |        Owner:  rlm
           Type:  enhancement        |       Status:  needs_review
       Priority:  major              |    Milestone:  sage-5.13
      Component:  memleak            |   Resolution:
       Keywords:                     |    Merged in:
        Authors:  Simon King, Nils   |    Reviewers:  Simon King
  Bruin                              |  Work issues:
Report Upstream:  None of the above  |       Commit:
  - read trac for reasoning.         |  851cc9522dde332561101f1c84182a0a84b8eed4
         Branch:                     |     Stopgaps:
  u/SimonKing/ticket/13394           |
   Dependencies:                     |
-------------------------------------+-------------------------------------

Comment (by SimonKing):

 Bad news. When trying to turn your stress test into an indirect test,
 things didn't work for me:
 {{{
 sage: B=3
 sage: L = [None]*B
 sage: D1 = WeakValueDictionary()
 sage: D2 = WeakValueDictionary()
 sage: import gc
 sage: for i in range(100):
 ....:     ki = floor(random()*B)
 ....:     vi = C(floor(random()*B))
 ....:     print "assign",ki,id(vi),"to D1"
 ....:     D1[ki] = vi
 ....:     print "assign",ki,id(vi),"to D2"
 ....:     D2[ki] = vi
 ....:     print "assign",ki,id(vi),"to L"
 ....:     L[ki]  = vi
 ....:     ko = floor(random()*B)
 ....:     if ko in D1:
 ....:         print "delete",ko,id(L[ko]),"from D1"
 ....:         del D1[ko]
 ....:         print "delete",ko,id(L[ko]),"from L"
 ....:         L[ko] = None
 ....:         _ = gc.collect()
 ....:     assert D1 == D2
 assign 2 220846028 to D1
 assign 2 220846028 to D2
 assign 2 220846028 to L
 assign 0 220845964 to D1
 assign 0 220845964 to D2
 assign 0 220845964 to L
 assign 1 220845932 to D1
 assign 1 220845932 to D2
 assign 1 220845932 to L
 delete 0 220845964 from D1
 delete 0 220845964 from L
 bye-bye 220845964
 assign 2 221294892 to D1
 assign 2 221294892 to D2
 assign 2 221294892 to L
 bye-bye 220846028
 delete 1 220845932 from D1
 delete 1 220845932 from L
 bye-bye 220845932
 assign 0 221294764 to D1
 assign 0 221294764 to D2
 assign 0 221294764 to L
 delete 2 221294892 from D1
 delete 2 221294892 from L
 bye-bye 221294892
 assign 1 221294700 to D1
 assign 1 221294700 to D2
 assign 1 221294700 to L
 delete 1 221294700 from D1
 delete 1 221294700 from L
 Traceback (most recent call last):
 ...
 AssertionError:
 }}}

 Do you think this is from a design flaw (race condition) in my test? Or do
 you think it is a genuine problem of our code?

 To my understanding, deletion of a value (and thus execution of the
 callback) should happen immediately after replacing the value on the list
 `L`: Cyclic garbage collection is not involved, and hence there should be
 no delay. However, just to be on the safe side, I explicitly added a
 garbage collection.

 Or did I misunderstand how deallocation of Python objects without
 reference cycles is working? Can there be a delay?

--
Ticket URL: <http://trac.sagemath.org/ticket/13394#comment:73>
Sage <http://www.sagemath.org>
Sage: Creating a Viable Open Source Alternative to Magma, Maple, Mathematica, 
and MATLAB

-- 
You received this message because you are subscribed to the Google Groups 
"sage-trac" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sage-trac.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to