Fix exception propagation from module init and memory leak#175
Open
elmopl wants to merge 4 commits intoboostorg:developboostorg/python:developfrom
Open
Fix exception propagation from module init and memory leak#175elmopl wants to merge 4 commits intoboostorg:developboostorg/python:developfrom
elmopl wants to merge 4 commits intoboostorg:developboostorg/python:developfrom
Conversation
Unfortunately due to optimised build of Python3 libraries and executable I got only partial stack from [http://clang.llvm.org/docs/AddressSanitizer.html], however digging into and reducing my code I tracked it down to be issue with `boost/libs/python/src/object/enum.cpp`. It has to bits that leak (and comment mentioning there is one): PyObject *mod = PyObject_GetAttrString( self_, "__module__"); Leaks reference, as it never decreases it. It also stores a new string object under object's `name` that ref count never gets decremented. That commit fixes both issues.
When exception gets set we need to return NULL in Python3 as module pointer.
Code in `Python/importdl.c` explicitly checks for this and suppresses exception (so you lose any knowledge of what went wrong):
PyObject *
_PyImport_LoadDynamicModuleWithSpec(PyObject *spec, FILE *fp)
{
[...]
m = p0();
_Py_PackageContext = oldcontext;
if (m == NULL) {
if (!PyErr_Occurred()) {
PyErr_Format(
PyExc_SystemError,
"initialization of %s failed without raising an exception",
name_buf);
}
goto error;
} else if (PyErr_Occurred()) {
PyErr_Clear();
PyErr_Format(
PyExc_SystemError,
"initialization of %s raised unreported exception",
name_buf);
m = NULL;
goto error;
}
In above the first case propagates actually raised exception, but only if we returned NULL.
Otherwise it overrides it with very unhelpful message of "unreported exception".
Fix is to simply return NULL if we got exception raised inside `init_function`.
Handling of that case follows what I could find in `Modules/symtablemodule.c` bundled with Python:
PyMODINIT_FUNC
PyInit__symtable(void)
{
PyObject *m;
[...]
if (PyErr_Occurred()) {
Py_DECREF(m);
m = 0;
}
return m;
}
So it checks if exception was raised, and if so it frees module and returns NULL.
`__cxa_demangle` returns a new pointer that needs to be freed. As much as this is only a minor leak (tenths of bytes per each unique symbol that got resolved) it makes automated memory leak checking tools quite upset. This simply adds a helper that frees memory for names we've kept. Also, removed a lot of trailing whitespace.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Two fixes:
type_id.cpp:gcc_demangle(see details in 1d19e6f)