[issue1540] Refleak tests: test_doctest and test_gc are failing

Amaury Forgeot d'Arc report at bugs.python.org
Wed Dec 5 17:51:42 CET 2007


Amaury Forgeot d'Arc added the comment:

Finally I found a potential problem with the garbage collector in a
specific case:
- Some object C participates in a reference cycle, and contains a
reference to another object X.
- X.__del__ 'resurrect' the object, by saving its 'self' somewhere else.
- X contains a reference to Y, which has a __del__ method.
When collecting all this, gc.garbage == [Y] !
This is not true garbage: if you clean gc.garbage, then the next
gc.collect() clears everything.


Now, try to follow my explanation (if I correctly understand the gc
internals):
- When the cycle is garbage collected, X and Y are detected as
'unreachable with finalizers', and put in a specific 'finalizers' list.
- the cycle is broken, C is deallocated. 
- This correctly triggers X.__del__. X is removed from the 'finalizers'
list.
- when X is resurrected, it comes back to the normal gc tracking list.
- At the end, 'finalizers' contains 3 objects: X.__dict__, Y and Y.__dict__.
- Y is then considered as garbage.

I join a script which reproduces the behaviour. Note that 2.4 and 2.5
are affected too.
In py3k, the 'resurrect' seems to be caused by a (caught) exception in
TextIOWrapper.close(): the exception contains a reference to the frame,
which references the self variable.

----------
components: +Interpreter Core
nosy: +gvanrossum
Added file: http://bugs.python.org/file8880/gc_bug.py

__________________________________
Tracker <report at bugs.python.org>
<http://bugs.python.org/issue1540>
__________________________________
-------------- next part --------------
A non-text attachment was scrubbed...
Name: gc_bug.py
Type: text/x-python
Size: 651 bytes
Desc: not available
Url : http://mail.python.org/pipermail/python-bugs-list/attachments/20071205/bc4aa8c9/attachment.py 


More information about the Python-bugs-list mailing list