[3.7] bpo-38070: visit_decref() calls _PyObject_IsFreed() in debug mode by vstinner · Pull Request #16816 · python/cpython (original) (raw)

@ned-deily: "What bug is this backport trying to solve in 3.7? This seems like a new feature and one that would not be appropriate in 3.7 especially at this stage in its life cycle."

I adjusted the PR title.

With this change, when Python is built in debug mode, Python can now detects some bugs in C extensions during a garbage collection. Bugs in visit_decref() are the most complex to debug, the function can crash for various reasons. This change helps to detect some kinds of visit_decref() bugs.

My notes on visit_decref():
https://pythondev.readthedocs.io/debug_tools.html#debug-crash-in-garbage-collection-visit-decref

This change is a minimalist and simplified backport of the work I did in the master branch.

The change has no effect when Python is built in release mode. In debug mode, it only changes visit_decref() when it's called on an invalid object. Without this change, Python will crash anyway in this case. For example, deferencing a NULL pointer will crash. Same for op=0xFDFDFDFDFDFDFDFD.

I'm not sure if it makes sense to backport the change to Python 3.7, since it's not trivial to replace a release Python with a debug Python: the ABI is not compatible and C extensions must be recompiled. I'm targeting bugs in third party code, not really bugs in CPython (even if the change will benefit to both).

@pablogsal: What do you think of this change for Python 3.7?