Skip to content

bpo-38009: Do not call weakref callbacks that are being collected #15645

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Include/cpython/objimpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ typedef struct {
#define _PyGC_SET_FINALIZED(o) \
_PyGCHead_SET_FINALIZED(_Py_AS_GC(o))

PyAPI_FUNC(int) _PyObject_GC_IS_COLLECTING(PyObject *op);

PyAPI_FUNC(PyObject *) _PyObject_GC_Malloc(size_t size);
PyAPI_FUNC(PyObject *) _PyObject_GC_Calloc(size_t size);
Expand Down
14 changes: 13 additions & 1 deletion Modules/gcmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,22 @@ module gc
/* Get the object given the GC head */
#define FROM_GC(g) ((PyObject *)(((PyGC_Head *)g)+1))

#define _PyGCHead_IS_COLLECTING(o) \
(((o)->_gc_prev & PREV_MASK_COLLECTING) != 0)

int _PyObject_GC_IS_COLLECTING(PyObject *op){
if (PyObject_IS_GC(op)) {
PyGC_Head *gc = AS_GC(op);
return _PyGCHead_IS_COLLECTING(gc);
} else {
return 0;
}
}

static inline int
gc_is_collecting(PyGC_Head *g)
{
return (g->_gc_prev & PREV_MASK_COLLECTING) != 0;
return _PyGCHead_IS_COLLECTING(g);
}

static inline void
Expand Down
30 changes: 30 additions & 0 deletions Objects/weakrefobject.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "Python.h"
#include "objimpl.h"
#include "structmember.h"


Expand Down Expand Up @@ -874,6 +875,35 @@ PyWeakref_GetObject(PyObject *ref)
static void
handle_callback(PyWeakReference *ref, PyObject *callback)
{
/* If the garbage collector support is not properly implemented on
* some classes that are involved in a reference cycle, a weak
* reference may try to invoke a callback object that is being
* cleaned (tp_clear) by the garbage collector and it may be in an
* inconsistent state. As the garbage collector explicitly does
* not invoke callbacks that are part of the same cycle isolate (check
* PEP 442 for references about this terminology) as the weak reference
* (pretending that the weak reference was * destroyed first), we
* should act in the same way here.
*
* When running the garbage collector pass over a generation, is
* possible to end in this function if the tp_clear of a function
* decrements the references of some internal object that is
* weak-referenced, invoking the weak reference callback that will
* try to call the function, which is in an incosistent state as
* is in the middle of its tp_clear and some internal fields may
* be NULL. */

if (PyObject_IS_GC(callback) && _PyObject_GC_IS_COLLECTING(callback)) {
PyErr_WarnEx(PyExc_RuntimeWarning, "A weak reference"
" was trying to execute a callback to a function that is being cleared by"
" the garbage collector.\n A C extension class in the dependence"
" chain is probably not implementing the garbage collector support"
" correctly.", 1);
/* Return to avoid a potential crash when calling the callback that is in
* an invalid state */
return;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the actual effect of just returning here? The callback isn't being called, but it wouldn't have been called had all the objects in the cycle had done the right thing, right? So what's the actual value of the warning?

Copy link
Member Author

@pablogsal pablogsal Sep 16, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So what's the actual value of the warning?

The reason we return is to not crash and then we warn that the interpreter has avoided a crash due to a bad implementation but we need to issue the warning so this does not pass silently.

Would you prefer to just crash?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I'm trying to find out if there are other reasons that people should fix their code, besides the specific crash that you're now turning into a warning. If the only reason to fix the code is to avoid this warning, the warning just becomes noise -- especially because very often the warning isn't seen by the developer responsible for the incorrect C code, but some helpless user.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I understand. Well, after this fix there is no hard crash, but an incorrect implementation of the garbage collector can have undefined consequences. These range from leaks because tp_traverse is not correctly implemented or some other crashes elsewhere.

What do you think we should do here? Maybe a more comprehensive message?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thinking less of a message, really :) The message is useful for people who have control over the C code that does the wrong thing, but to end users of the module. Maybe the exception should be a DeprecationWarning, so it's not shown by default? Perhaps we should discuss this on python-dev or discourse.

}

PyObject *cbresult = _PyObject_CallOneArg(callback, (PyObject *)ref);

if (cbresult == NULL)
Expand Down