On 8 Jun 2018, at 12:36, Serhiy Storchaka <storchaka@gmail.com> wrote:

08.06.18 11:31, Victor Stinner пише:
Do you suggest to trigger a fake "GC collection" which would just
visit all objects with a no-op visit callback? I like the idea!

Yeah, that would help to detect objects in an inconsistent state and
reuse the existing implemented visit methods of all types.

Would you be interested to try to implement this new debug feature?

It is simple:

#ifdef Py_DEBUG
void
_PyGC_CheckConsistency(void)
{
    int i;
    if (_PyRuntime.gc.collecting) {
        return;
    }
    _PyRuntime.gc.collecting = 1;
    for (i = 0; i < NUM_GENERATIONS; ++i) {
        update_refs(GEN_HEAD(i));
    }
    for (i = 0; i < NUM_GENERATIONS; ++i) {
        subtract_refs(GEN_HEAD(i));
    }
    for (i = 0; i < NUM_GENERATIONS; ++i) {
        revive_garbage(GEN_HEAD(i));
    }
    _PyRuntime.gc.collecting = 0;
}
#endif

Wouldn’t it be enough to visit just the the newly tracked object in PyObject_GC_Track with a visitor function that does something minimal to verify that the object value is sane, for example by checking PyType_Ready(Py_TYPE(op)).

That would find issues where objects are tracked before they are initialised far enough to be save to visit, without changing GC behavior. I have no idea what the performance impact of this is though.

Ronald