• Jason Rhinelander's avatar
    Fix segfault when reloading interpreter with external modules (#1092) · 326deef2
    Jason Rhinelander authored
    * Fix segfault when reloading interpreter with external modules
    
    When embedding the interpreter and loading external modules in that
    embedded interpreter, the external module correctly shares its
    internals_ptr with the one in the embedded interpreter.  When the
    interpreter is shut down, however, only the `internals_ptr` local to
    the embedded code is actually reset to nullptr: the external module
    remains set.
    
    The result is that loading an external pybind11 module, letting the
    interpreter go through a finalize/initialize, then attempting to use
    something in the external module fails because this external module is
    still trying to use the old (destroyed) internals.  This causes
    undefined behaviour (typically a segfault).
    
    This commit fixes it by adding a level of indirection in the internals
    path, converting the local internals variable to `internals **` instead
    of `internals *`.  With this change, we can detect a stale internals
    pointer and reload the internals pointer (either from a capsule or by
    creating a new internals instance).
    
    (No issue number: this was reported on gitter by @henryiii and @aoloe).
    326deef2
external_module.cpp 554 Bytes