Python extension module with callbacks into Python

Paul Grinberg gri6507 at gmail.com
Wed Oct 28 09:25:41 EDT 2020


> > I am running into unpredictable behavior with my Python extension module that wraps around a C++ library that starts a new pthread and, after doing some work, generates callbacks back into the caller. I've greatly simplified this to a simplistic example which still demonstrates this problem. The following will sometimes generate a Fatal Python error: PyEval_SaveThread: NULL tstate, usually rather quickly. Sometimes it SIGSEGV on tupledealoc. Occasionally this deadlocks. I am at a loss why. Does anyone have any ideas?

> You did not say what OS and python version you are working with. 

I apologize. I am running this on Ubuntu 18.04 under Python 3.6 (default for this Ubuntu's Bionic release) fully updated to the latest provided by Ubuntu's standard repos, which is 3.6.9-1~18.04ubuntu1.3. After posting this question here I continued investigating this problem and found that when I repeat the same test under Python 3.7.3 or 3.8.5 (installed via PyEnv), there are no issues. Then, I went back to the most recent 3.6 release which is 3.6.12 (also installed via PyEnv) and again saw the issue. This seems to suggest that the problem is in 3.6.x Python.  

> Why do you need to call PyEval_ThreadsInitialized()? its deprecated. 
> 
> PyInitialize sets up threads unconditionally since 3.7. 

I did not realize that PyEval_ThreadsInitialized() is deprecated. However, I was also running under 3.6. I will move away from that once I move to a newer version of Python



More information about the Python-list mailing list