This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: [C API] Py_NewInterpreter() cannot be called from a thread which has no Python thread state
Type: Stage:
Components: C API, Subinterpreters Versions: Python 3.10
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: samuel-thibault, vstinner
Priority: normal Keywords:

Created on 2021-04-09 13:55 by vstinner, last changed 2022-04-11 14:59 by admin.

Files
File name Uploaded Description Edit
newinter.c vstinner, 2021-04-09 13:55
Messages (1)
msg390626 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2021-04-09 13:55
Build attached newinter.c C extension. Example:

$ gcc -shared newinter.c $(pkg-config python-3.10 --cflags --libs) -o newinter.so -fPIC


Trying to load the C extension crashs in various ways. Crashes on a Python debug build:

(1)

$ PYTHONPATH=$PWD ./python -c 'import newinter'
create new thread
Modules/gcmodule.c:113: gc_decref: Assertion "gc_get_refs(g) > 0" failed: refcount is too small
Enable tracemalloc to get the memory block allocation traceback

object address  : 0x7f1a1163c8f0
object refcount : 1
object type     : 0x871020
object type name: dict
object repr     : {}

Fatal Python error: _PyObject_AssertFailed: _PyObject_AssertFailed
Python runtime state: finalizing (tstate=0x0000000001e0d390)

Current thread 0x00007f1a115a0640 (most recent call first):
  File "<frozen importlib._bootstrap>", line 241 in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 855 in exec_module
  File Exception ignored deletion of interned string failed"<frozen importlib._bootstrap>", line 688 in _load_unlocked
  File "<frozen importlib._bootstrap>", line 1006 in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 1027 in _find_and_load
  File "<frozen importlib._bootstrap>", line 1207 in _install_external_importers
Segmentation fault (core dumped)

(2)

$ PYTHONPATH=$PWD ./python -c 'import newinter'
create new thread
Objects/unicodeobject.c:15769: _Py_NegativeRefcount: Assertion failed: object has negative ref count
Enable tracemalloc to get the memory block allocation traceback

object address  : 0x7fe124af08e0
object refcount : -1
object type     : 0x8797e0
object type name: str
object repr     : Debug memory block at address p=0x7fe124af08e0: API 'o'
    60 bytes originally requested
    The 7 pad bytes at p-7 are FORBIDDENBYTE, as expected.
    The 8 pad bytes at tail=0x7fe124af091c are not all FORBIDDENBYTE (0xfd):
        at tail+0: 0x72 *** OUCH
        at tail+1: 0x5f *** OUCH
        at tail+2: 0x63 *** OUCH
        at tail+3: 0x61 *** OUCH
        at tail+4: 0x63 *** OUCH
        at tail+5: 0x68 *** OUCH
        at tail+6: 0x65 *** OUCH
        at tail+7: 0xfd
    Data at p: ff ff ff ff ff ff ff ff ... 61 6b 72 65 66 5f 5f 65<refcnt -1 at 0x7fe124af08e0>

Enable tracemalloc to get the memory block allocation traceback

Fatal Python error: _PyMem_DebugRawFree: bad trailing pad byte
Python runtime state: finalizing (tstate=0x0000000001564390)

Current thread 0x00007fe1321ff740 (most recent call first):
<no Python frame>

Extension modules: newinter (total: 1)
Aborted (core dumped)

(3)

$ PYTHONPATH=$PWD ./python -c 'import newinter'
create new thread
Exception ignored deletion of interned string failedFatal Python error: _PyInterpreterState_GET: the function must be called with the GIL held, but the GIL is released (the current Python thread state is NULL)
Python runtime state: finalizing (tstate=0x0000000000c51390)

Current thread 0x00007f329308a640 (most recent call first):
  File "<frozen importlib._bootstrap>", line 241 in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 855 in exec_module
  File "<frozen importlib._bootstrap>", line 688 in _load_unlocked
  File "<frozen importlib._bootstrap>", line 1006 in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 1027 in _find_and_load
  File "<frozen importlib._bootstrap>", line 1207 in _install_external_importers
Aborted (core dumped)

(4)

$ PYTHONPATH=$PWD ./python -c 'import newinter'
create new thread
Fatal Python error: _Py_CheckSlotResult: Slot __setitem__ of type dict succeeded with an exception set
Python runtime state: finalizing (tstate=0x0000000000f0e390)

Thread 0xTraceback (most recent call last):
00007fb4a331b640 (most recent call first):
<no Python frame>
Aborted (core dumped)
History
Date User Action Args
2022-04-11 14:59:44adminsetgithub: 87959
2021-04-16 09:51:39vstinnersetcomponents: + Subinterpreters
2021-04-09 16:13:54samuel-thibaultsetnosy: + samuel-thibault
2021-04-09 13:55:22vstinnercreate