| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
Modules/gcmodule.c contains various assertions which can fail due to reference counting errors elsewhere in either python, or an extension module. These can be difficult to track down.
In the hope of maximizing the information from crash reports, the attached patch (against py3k) introduces a new assertion macro to Objects/object.h and Objects.c, which provides a richer debug message. In particular, it identifies which object has the issue, and can more clearly spell out the problem.
The patch replaces all uses of assert() in Modules/gcmodule.c for which a specific object has an issue (e.g. bogus reference count).
The implementation may play somewhat fast-and-loose with rules about object invariants: you might have an only partially valid object, but the process is about to abort, so it seems acceptable to try to glean extra information on stderr. (This may turn an abort into a segfault, of course)
Caveats:
Thoughts?
Attaching a simplified version of the patch; I got rid of the callbacks.
Still doesn't have test cases.
I suspect that the use of __STRING and __PRETTY_FUNCTION__ may be compatibility issues. I believe that __FILE__ and __LINE__ and standard C though.
Attaching updated version of the patch.
I've added a selftest which (in a sacrificial subprocess) abuses ctypes to break an ob_refcnt, and then triggers a garbage collection.
I also changed the printing to stderr to directly use fprintf and fflush to ensure that data leaves the process before abort kills it (not sure if this is a cross-platform or unicode no-no, though).
As above, but I added an extra call to fflush in case the call to _PyObject_Dump leads to a segfault.
How about turning these asserts into Py_FatalError()s and then enabling Victor's faulthandler extension?
I'd suggest calling Py_FatalError rather than calling abort() directly in _PyObject_AssertFailed, but otherwise this looks like a nice improvement over standard C asserts for state invariants that may be broken by buggy C extensions.
For the tests, take a look at test.script_helper - it provides some convenience wrappers for spawning subprocesses for tests that would cause problems if run in the current process.
I'm currently patching Python 3.3.2 with this, so I thought it might be nice to attach an up-to-date patch. The only notable difference is that I added self.preclean() at the beginning of test_refcount_errors - without it, running test suite produced a huge number of these lines:
Exception AttributeError: "'GCCallbackTests' object has no attribute 'visit'" in <bound method GCCallbackTests.cb1 of <main.GCCallbackTests testMethod=test_refcount_errors>> ignored
00170-gc-assertions.patch is used in the Fedora package of Python.
I converted the patch to a pull request: PR 10062.
My PR only uses the new assertion macro once. I plan to write more changes to use the new assertion macros in more places, once the first PR is merged.
I pushed the change and even more, so I consider that the issue can now be closed... 8 years later! Thank you very much Dave Malcolm for this nice idea, and for its implementation. Thanks Bohuslav "Slavek" Kabrda for the rebase in 2013, and thanks to my colleagues who rebased the patch frequently since 2013 in the Fedora package!
Maybe some people (like me?) want to use _PyObject_ASSERT() in more places, but I consider that we don't need to leave this issue open just for that.
I took the 00170-gc-assertions.patch rebased on Python 3.7.1 by my colleagues for the Fedora package, and I rebased it on master. I modified more functions in object.c and typeobject.c to use _PyObject_ASSERT(). I tried to not replace all assert(), but only when it's revelant.
I added code to detect if the object memory has been freed to avoid derefering 0xdbdbdbdbdbdbdbdb pointers which is very likely to cause a segmantation fault. It should reduce the risk of crash when dumping the faulty object.
Dave Malcolm:
- Only tested on gcc-4.4.3 so far; the __STRING(expr) and __PRETTY_FUNCTION__ look non-portable.
I used Py_STRINGIFY() and __func__ in the final patch. __func__ is part of the C99 standard which is now required since Python 3.6: see PEP-7.
Dave Malcolm:
- no test case; I thought about using ctypes to extract PyObject_IncRef from the implementation, but this is likely to lead to brittle test cases. Alternatively, is xxmodule to be used for this kind of thing?
I reworked the unit test to not use ctypes, but write the crashing code in C instead.
Antoine Pitrou:
How about turning these asserts into Py_FatalError()s and then enabling Victor's faulthandler extension?
Done.
Thanks!
| Back | FazBrowse Home | New Git URL |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more detailsGitHub fields:
bugs.python.org fields:
Linked PRs