multiprocessing.c currently has code like this: temp = PyDict_New(); if (!temp) return; if (PyModule_AddObject(module, "flags", temp) < 0) return; PyModule_AddObject consumes the reference to temp, so it could conceivable be deleted before the rest of this function finishes.
I believe this patch solves the problem. I added the line Py_DECREF(temp) after the code block shown above. I also changed the line if (PyDict_SetItemString(temp, #name, Py_BuildValue("i", name)) < 0) return to if (PyDict_SetItemString(PyObject_GetAttrString(module, "flags"), #name, Py_BuildValue("i", name)) < 0) return
This doesn't look right. PyDict_SetItemString doesn't steal the references passed to it, so your reference to flags will be leaked each time. Besides, I think it's a little cleaner to INCREF it before call PyModule_AddObject, then DECREF it at any point you return. Additionally, I've just noticed that the result of Py_BuildValue is getting leaked. It should be stored to a temporary, added to flags, then the temporary should be released.