This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: weakref callbacks and gc corrupt memory
Type: Stage:
Components: Interpreter Core Versions: Python 2.3
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: tim.peters Nosy List: theller, tim.peters
Priority: release blocker Keywords:

Created on 2003-11-12 17:05 by theller, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
crash.py theller, 2003-11-12 17:08 Script triggering the error
stacktrace.txt theller, 2003-11-13 07:49 stackdump.txt
Messages (14)
msg18974 - (view) Author: Thomas Heller (theller) * (Python committer) Date: 2003-11-12 17:05
This program crashes the Python interpreter in a debug
build, and probably corrupts memory in release build
(on Windows, at least):

"""
import weakref
import gc

_boundMethods = weakref.WeakKeyDictionary()

def safeRef(object):
    selfkey = object.im_self
    funckey = object.im_func
    _boundMethods[selfkey] = weakref.WeakKeyDictionary()
    _boundMethods[selfkey][funckey] =
BoundMethodWeakref(object)

class BoundMethodWeakref:
    def __init__(self, boundMethod):
        def remove(object):
            gc.collect()
        self.weakSelf =
weakref.ref(boundMethod.im_self, remove)

class X(object):
    def test(self):
        pass

def test():
    print "A"
    safeRef(X().test)
    print "B"

if __name__ == "__main__":
    test()
"""

See also these messages:

http://mail.python.org/pipermail/python-dev/2003-November/040189.html

http://mail.python.org/pipermail/python-dev/2003-November/040191.html
msg18975 - (view) Author: Thomas Heller (theller) * (Python committer) Date: 2003-11-12 17:07
Logged In: YES 
user_id=11105

IMO this is a serious problem because it renders weakref
callbacks basically unusable - gc can occur at any time.
So I raised the priority to 9.
msg18976 - (view) Author: Thomas Heller (theller) * (Python committer) Date: 2003-11-12 17:08
Logged In: YES 
user_id=11105

Attached the script triggering the error.
msg18977 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2003-11-12 19:54
Logged In: YES 
user_id=31435

I agree this is a critical bug; assigned to me.
msg18978 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2003-11-12 20:47
Logged In: YES 
user_id=31435

Back to you, Thomas!  Please check your real app against 
current CVS Python.  I checked in a putative fix as

Lib/test/test_weakref.py; new revision: 1.29
Misc/NEWS; new revision: 1.890
Objects/typeobject.c; new revision: 2.251

The new test case in test_weakref.py is very much simpler 
than what you've been running, so I want confirmation that 
the real problem is fixed too before closing this.

Please record what happens here, then assign back to me.
msg18979 - (view) Author: Thomas Heller (theller) * (Python committer) Date: 2003-11-13 07:49
Logged In: YES 
user_id=11105

Thanks for the fast response, Tim, but this doesn't seem to
be the (complete) solution.

The debug build crashes far less than before, but it still
occurs, and in the same way.  I attach a complete MSVC6
stack dump.  The function names you do not know come from
the ctypes extension.

Back to you again :-(

Oh, what helped for me was to disable gc in the weakref
callback functions.  Don't know if that is an option for the
C code...
msg18980 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2003-11-13 13:56
Logged In: YES 
user_id=31435

Thanks!  There's more than one bug here, of course.

When you say "what helped for me was ...", do you mean 
that cured *all* the problems you're seeing, or just that "it 
helped", in the ordinary sense that it cured more of the 
problems, but not all of the problems.

Since I don't have a failing program I can run at this point, I 
can't test any other ideas.  You <wink> can, though:

In typeobject.c's subtype_dealloc, on the trunk, right before 
the comment

/*  Clear slots up to the nearest base with a different 
tp_dealloc */

try inserting this line:

    self->ob_refcnt = 1;

and right after the

	/* Call the base tp_dealloc() */

comment insert

    assert(self->ob_refcnt == 1);
    self->ob_refcnt = 0;

That should cure the more-complicated (than in your original 
report) bug in the new stacktrace.  However, it *may* cause 
some other code to die with an assert, bitching about the 
suddenly non-zero refcnt.  I can't guess whether it will; my 
best guess is that it won't.
msg18981 - (view) Author: Thomas Heller (theller) * (Python committer) Date: 2003-11-13 21:06
Logged In: YES 
user_id=11105

> When you say "what helped for me was ...", do you mean
> that cured *all* the problems you're seeing, or just that "it
> helped", in the ordinary sense that it cured more of the
> problems, but not all of the problems.

Well, it seemed to help "completely", I never observed the
crash with this workaround.

For the patch to subtype_dealloc, I will try your suggestion
and report.
msg18982 - (view) Author: Thomas Heller (theller) * (Python committer) Date: 2003-11-13 21:13
Logged In: YES 
user_id=11105

Applied your patch, very cool!  This problem doesn't occur
any more, it seems.  But this was only after a short test
with my application.
I will patch the 2.3 branch and test more extensively with
this one tomorrow.
msg18983 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2003-11-13 22:23
Logged In: YES 
user_id=31435

Good!  I hoped it would fix it, but it's not the best way to fix 
it.

I've checked in a better way, so please test against the 
current CVS trunk.  I'll backport that to the 2.3 maint branch 
too, but later today.
msg18984 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2003-11-13 22:55
Logged In: YES 
user_id=31435

What I hope is the last fix for this class of bug has been 
checked in on release23-maint too, so feel free to use that 
for testing instead.
msg18985 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2003-11-19 02:39
Logged In: YES 
user_id=31435

Ping.  Any progress, Thomas?  Reduced priority a notch due 
to inactivity.
msg18986 - (view) Author: Thomas Heller (theller) * (Python committer) Date: 2003-11-19 16:58
Logged In: YES 
user_id=11105

The problem doesn't seem to occur anymore in a
release23-maint build. Thanks.
msg18987 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2003-11-19 21:57
Logged In: YES 
user_id=31435

Thank you!  Closing as fixed.  Note that even funkier changes 
to fix (other) weakref bugs are in the queue, so I hope you 
try this particular app against 2.3 maint again (maybe next 
week).
History
Date User Action Args
2022-04-11 14:56:01adminsetgithub: 39539
2003-11-12 17:05:47thellercreate