Repository navigation
Legacy-mode subinterpreters in Python 3.12: import _tkinter leads to shutdown crash #115649
Description
Activity
- addedtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
on Feb 19, 2024 I can reproduce the issue with this Python code.
import _xxsubinterpreters as interpreters interp0 = interpreters.create(isolated = 0) interp1 = interpreters.create(isolated = 0) interpreters.run_string(interp0, 'import _tkinter') interpreters.run_string(interp1, 'import _tkinter') interpreters.destroy(interp0) interpreters.destroy(interp1) print('Successful!')
Funnily enough, swapping the two
destroycalls:interpreters.destroy(interp1) interpreters.destroy(interp0)
makes everything work as intended.
I suspect, since this crash looks very similar to other crashes I've seen doing the same thing like #112292 and #112140 that
_tkinterhas single-phase init and/or global state so the first time the module is unloaded certain global state objects are deallocated and then the second time it crashes because tuple items refer to things that don't exist.There's a longer list of them here #112677
Why didn't this crash in older versions? because the sub interpreters implementation wasn't completed until 3.12 and so the state was global and not per-interpreter anyway.
I'm assuming that if you set
isolated=1it will tell you that tkinter isn't supported?I think you should get that warning as well even with non-isolated sub interpreters since it will cause this type of crash CC @ericsnowcurrently
- addedtype-crashA hard crash of the interpreter, possibly with a core dumpA hard crash of the interpreter, possibly with a core dumpand removedtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
on Feb 27, 2024 In the main branch (c3b6dbf), no mater the destroy order, the interpreter will crash, this is a minimal code to reproduce it:
import _interpreters iid = _interpreters.create() _interpreters.exec(iid, "import _tkinter") _interpreters.destroy(iid)
$ ./python c.py Debug memory block at address p=0x7899b89e3520: API '�' 15987178197214944733 bytes originally requested The 7 pad bytes at p-7 are not all FORBIDDENBYTE (0xfd): at p-7: 0xdd *** OUCH at p-6: 0xdd *** OUCH at p-5: 0xdd *** OUCH at p-4: 0xdd *** OUCH at p-3: 0xdd *** OUCH at p-2: 0x00 *** OUCH at p-1: 0x00 *** OUCH Because memory is corrupted at the start, the count of bytes requested may be bogus, and checking the trailing pad bytes may segfault. The 8 pad bytes at tail=0xddde5677967c12fd are fish: Job 1, './python c.py' terminated by signal SIGSEGV (Address boundary error)I guess a lot codes have changed, so the original issue may be fixed, but another bug was introduces in #118157.
- added a commit that references this issue
on Jun 17, 2024 - added a commit that references this issue
on Jun 17, 2024 Is this still a problem? I just tried to reproduce the crash against main and it worked fine.
I bisected with the reproducer and it seems to be fixed by 291cfa454b9c5b677c955aaf53fab91f0186b6fa.
Reacted by Eric Snow and An Long
Metadata
Metadata
Assignees
Labels
Projects
- StatusShow more project fieldsDone
Bug report
Bug description:
Hello everyone,
I am working on an application embedding multiple Python subinterpreters - for which the Python version should be upgraded from Python 3.10 to Python 3.12.1. For the time being, the "legacy version" of subinterpreters (i.e., using a global, shared GIL) should be used, since all Python extensions (including
_tkinterand all extensions with single-phase initialization) should be supported.If I understand the docs correctly, using the legacy
Py_NewInterpreter()method should preserve the existing behavior. Still, the following application crashes at shutdown:When compiling and running this program with a debug build of Python 3.12.1 (or later) on Linux, I get this output:
With a non-debug build, the program exits with a segmentation fault.
The
gdbbacktrace looks as follows:Digging further into the backtrace, it looks like the Python garbage collector is trying to decrease the reference counter to the
_tkintermodule twice, despite it having been increased only once. Oddly enough, the program runs just fine when destroying the interpreters in reverse order:Can anyone help me shed some light into this issue? Is there anything I am overlooking?
CPython versions tested on:
3.12.1, 3.12.2, 3.13.0a4
Operating systems tested on:
Linux, Windows
Linked PRs