4590f72259
Currently, during runtime destruction, `_PyImport_Cleanup` is clearing the interpreter state before clearing out the modules themselves. This leads to a segfault on modules that rely on the module state to clear themselves up. For example, let's take the small snippet added in the issue by @DinoV : ``` import _struct class C: def __init__(self): self.pack = _struct.pack def __del__(self): self.pack('I', -42) _struct.x = C() ``` The module `_struct` uses the module state to run `pack`. Therefore, the module state has to be alive until after the module has been cleared out to successfully run `C.__del__`. This happens at line 606, when `_PyImport_Cleanup` calls `_PyModule_Clear`. In fact, the loop that calls `_PyModule_Clear` has in its comments: > Now, if there are any modules left alive, clear their globals to minimize potential leaks. All C extension modules actually end up here, since they are kept alive in the interpreter state. That means that we can't clear the module state (which is used by C Extensions) before we run that loop. Moving `_PyInterpreterState_ClearModules` until after it, fixes the segfault in the code snippet. Finally, this updates a test in `io` to correctly assert the error that it now throws (since it now finds the io module state). The test that uses this is: `test_create_at_shutdown_without_encoding`. Given this test is now working is a proof that the module state now stays alive even when `__del__` is called at module destruction time. Thus, I didn't add a new tests for this. https://bugs.python.org/issue38076 |
||
---|---|---|
.. | ||
clinic | ||
Python-ast.c | ||
README | ||
_warnings.c | ||
asdl.c | ||
ast.c | ||
ast_opt.c | ||
ast_unparse.c | ||
bltinmodule.c | ||
bootstrap_hash.c | ||
ceval.c | ||
ceval_gil.h | ||
codecs.c | ||
compile.c | ||
condvar.h | ||
context.c | ||
dtoa.c | ||
dup2.c | ||
dynamic_annotations.c | ||
dynload_aix.c | ||
dynload_dl.c | ||
dynload_hpux.c | ||
dynload_shlib.c | ||
dynload_stub.c | ||
dynload_win.c | ||
errors.c | ||
fileutils.c | ||
formatter_unicode.c | ||
frozen.c | ||
frozenmain.c | ||
future.c | ||
getargs.c | ||
getcompiler.c | ||
getcopyright.c | ||
getopt.c | ||
getplatform.c | ||
getversion.c | ||
graminit.c | ||
hamt.c | ||
import.c | ||
importdl.c | ||
importdl.h | ||
importlib.h | ||
importlib_external.h | ||
importlib_zipimport.h | ||
initconfig.c | ||
makeopcodetargets.py | ||
marshal.c | ||
modsupport.c | ||
mysnprintf.c | ||
mystrtoul.c | ||
opcode_targets.h | ||
pathconfig.c | ||
peephole.c | ||
preconfig.c | ||
pyarena.c | ||
pyctype.c | ||
pyfpe.c | ||
pyhash.c | ||
pylifecycle.c | ||
pymath.c | ||
pystate.c | ||
pystrcmp.c | ||
pystrhex.c | ||
pystrtod.c | ||
pythonrun.c | ||
pytime.c | ||
strdup.c | ||
structmember.c | ||
symtable.c | ||
sysmodule.c | ||
thread.c | ||
thread_nt.h | ||
thread_pthread.h | ||
traceback.c | ||
wordcode_helpers.h |
README
Miscellaneous source files for the main Python shared library