Author vstinner
Recipients dino.viehland, eric.snow, pablogsal, twouters, vstinner
Date 2020-01-16.16:47:11
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
I can reproduce the crash on Linux. I interrupt the problem with CTRL+c (why is it blocked? I don't know). Then it does crash.

First, _PyImport_Cleanup() does clear all modules including _struct.

Then, _PyImport_Cleanup() calls gc.collect() which finalize_garbage() which calls A.__del__().

Problem: at this point, the _struct became unusable.


Thread 1 "python" received signal SIGSEGV, Segmentation fault.
0x0000000000473f30 in PyModule_GetState (m=0x0) at Objects/moduleobject.c:565
565	    if (!PyModule_Check(m)) {
(gdb) py-bt
Traceback (most recent call first):
  <built-in method pack of module object at remote 0x7fffea99acb0>
  File "/home/vstinner/python/master/Lib/multiprocessing/", line 400, in _send_bytes
    header = struct.pack("!i", n)
  File "/home/vstinner/python/master/Lib/multiprocessing/", line 200, in send_bytes
    self._send_bytes(m[offset:offset + size])
  File "/home/vstinner/python/master/Lib/multiprocessing/", line 368, in put
  File "/home/vstinner/python/master/Lib/multiprocessing/", line 649, in close
  File "/home/vstinner/python/master/", line 7, in __del__

In debug mode, the crash occurs in s_pack() at:



#define _structmodulestate(o) ((_structmodulestate *)PyModule_GetState(o))

static struct PyModuleDef _structmodule;

#define _structmodulestate_global _structmodulestate(PyState_FindModule(&_structmodule))

#define PyStruct_Check(op) PyObject_TypeCheck(op, (PyTypeObject *)_structmodulestate_global->PyStructType)

The problem is "_structmodulestate_global->PyStructType": PyState_FindModule(&_structmodule) returns NULL, _structmodulestate() calls PyModule_GetState(NULL) which does crash.


The question is why the _struct module was cleared whereas there was still a reference to it. Is it part of a reference cycle?
Date User Action Args
2020-01-16 16:47:12vstinnersetrecipients: + vstinner, twouters, dino.viehland, eric.snow, pablogsal
2020-01-16 16:47:12vstinnersetmessageid: <>
2020-01-16 16:47:12vstinnerlinkissue38076 messages
2020-01-16 16:47:11vstinnercreate