[issue44441] Malformed PyImport_Inittab after re-initialization
New submission from kryheb : Hi all, I observed misbehavior trying to embed the Python interpreter into a C app. It seems that after re-initialization, PyImport_Inittab is malformed and points to the memory freed _PyImport_Fini2. Steps to reproduce: 1. Append embedded module 2. Initialize Python from config with run_filename 3. Run main with an infinite loop 4. Interrupt script execution with async call 5. Finalize Python 6. Repeat all above Observed behavior: The script is executed at first iteration, but re-initialization fails with an error: " Traceback (most recent call last): File "", line 1187, in _install_external_importers ModuleNotFoundError: No module named 'posix' Error: external importer setup failed " Head of modules list at fist run: -- Modules: -- #0 'posix' #1 'errno' #2 'pwd' -- and after re-initialization: -- Modules: -- #0 'P '' #1 'errno' #2 'pwd' -- An issue discovered on: Fedora 33 gcc (GCC) 10.3.1 20210422 (Red Hat 10.3.1-1) python3-devel.x86_64 3.9.5-2.fc33 Issue still exists on the latest main and on the rc 3.10 Source code to reproduce an issue in attachment. Best regards, Krystian Heberlein -- components: C API, Library (Lib) files: inittab-bug.c messages: 395985 nosy: kryheb priority: normal severity: normal status: open title: Malformed PyImport_Inittab after re-initialization versions: Python 3.10, Python 3.9 Added file: https://bugs.python.org/file50115/inittab-bug.c ___ Python tracker <https://bugs.python.org/issue1> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue44441] Malformed PyImport_Inittab after re-initialization
Change by kryheb : -- keywords: +patch pull_requests: +25353 stage: -> patch review pull_request: https://github.com/python/cpython/pull/26767 ___ Python tracker <https://bugs.python.org/issue1> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue44441] Malformed PyImport_Inittab after re-initialization
kryheb added the comment: Hi vstinner, I apologize for the inaccurate code sample. Please find a simplified example without threads `inittab-bug_no-threads.c` in attachments. Please notice that __main__.py has changed, there is no infinite loop anymore. Steps to reproduce: 1. Append embedded module 2. Initialize Python from config with run_filename 3. Run main 4. Finalize 5. Repeat all above Observed behavior: The script is executed at the first iteration, but re-initialization fails with a segmentation fault: gdb) r Starting program: /home/kheb/proj/tmp/pyc/a.out [Thread debugging using libthread_db enabled] Using host libthread_db library "/lib64/libthread_db.so.1". Run Python Script >>> This is the python script Python Script completed (0) Program received signal SIGSEGV, Segmentation fault. __strlen_avx2 () at ../sysdeps/x86_64/multiarch/strlen-avx2.S:65 65 VPCMPEQ (%rdi), %ymm0, %ymm1 Missing separate debuginfos, use: dnf debuginfo-install libxcrypt-4.4.20-2.fc33.x86_64 (gdb) bt #0 __strlen_avx2 () at ../sysdeps/x86_64/multiarch/strlen-avx2.S:65 #1 0x77d039f0 in PyUnicode_FromString ( u=0x ) at Objects/unicodeobject.c:2309 #2 0x77dbf049 in list_builtin_module_names () at ./Python/sysmodule.c:2056 #3 0x77dc1777 in _PySys_InitCore (tstate=tstate@entry=0x422ac0, sysdict=sysdict@entry=0x7fffea221070) at ./Python/sysmodule.c:2813 #4 0x77dc36ef in _PySys_Create (tstate=tstate@entry=0x422ac0, sysmod_p=sysmod_p@entry=0x7fffd9d8) at ./Python/sysmodule.c:3087 #5 0x77da9b32 in pycore_interp_init (tstate=0x422ac0) at Python/pylifecycle.c:824 #6 0x77da9c8e in pyinit_config (runtime=runtime@entry=0x77fbe820 <_PyRuntime>, tstate_p=tstate_p@entry=0x7fffdca8, config=config@entry=0x7fffdac0) at Python/pylifecycle.c:866 #7 0x77daba3e in pyinit_core (runtime=runtime@entry=0x77fbe820 <_PyRuntime>, src_config=src_config@entry=0x7fffdd40, tstate_p=tstate_p@entry=0x7fffdca8) at Python/pylifecycle.c:1029 #8 0x77dabb18 in Py_InitializeFromConfig (config=0x7fffdd40) at Python/pylifecycle.c:1214 #9 0x004012d3 in main (argc=1, argv=0x7fffdfc8) at inittab-bug_no-threads.c:45 I hope this example helps. Best regards, Krystian -- Added file: https://bugs.python.org/file50125/inittab-bug_no-threads.c ___ Python tracker <https://bugs.python.org/issue1> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue44441] Malformed PyImport_Inittab after re-initialization
kryheb added the comment: Thanks vstinner for your help and for taking care of this case! -- ___ Python tracker <https://bugs.python.org/issue1> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com