embedded Python: one thread hangs while trying to get global Python lock

Gernot Hillier ghillie at suse.de
Mon Feb 3 10:43:53 CET 2003


I experience a weird problem in my multithreaded telecommunication 
application with embedded Python. It does roughly the following:

- thread A is running continuously and reads a python script and starts a 
function def'd in there in regular intervals

- thread B is created on demand (when a call comes in), runs another python 
script, calls a function def'd in there and finishes after that. 

When thread A + B run at the same time, thread A suddenly hangs forever on 
the following operation:

(gdb) bt
#0  0x4026cea9 in sigsuspend () from /lib/libc.so.6
#1  0x4003bd48 in __pthread_wait_for_restart_signal () from 
#2  0x4003814b in pthread_cond_wait () from /lib/libpthread.so.0
#3  0x080eb25f in PyThread_acquire_lock ()
#4  0x080c8dff in PyEval_RestoreThread ()
#5  0x0806a984 in capisuite_disconnect (args=0x824544c) at 
#6  0x080a7963 in PyCFunction_Call ()
#7  0x080cb66d in eval_frame ()
#8  0x080cc04e in PyEval_EvalCodeEx ()
#9  0x080cd584 in fast_function ()
#10 0x080cb5b2 in eval_frame ()
#11 0x080cc04e in PyEval_EvalCodeEx ()
#12 0x0810373c in function_call ()
#13 0x08092b17 in PyObject_Call ()
#14 0x080cd219 in PyEval_CallObjectWithKeywords ()
#15 0x08092ace in PyObject_CallObject ()
#16 0x08066106 in PythonScript::run() (this=0x8163f80) at 
#17 0x08067fae in IdleScript::run() (this=0x8163f80) at idlescript.cpp:59
#18 0x40090ee1 in ost::ThreadImpl::ThreadExecHandler(ost::Thread*) () from 
#19 0x4009025c in ccxx_exec_handler.1 () from /usr/lib/libccgnu2-0.99.so.0
#20 0x400391b0 in pthread_start_thread () from /lib/libpthread.so.0

To explain the backtrace: 
- the def'd function is called (#15)
- it starts a function implemented in C (#6/#5)
- This function is doing: 

        // do some blocking operation
        Py_BLOCK_THREADS  <---------- This is line 422 where it hangs

So I firstly thought it was clear I made an error somewhere in thread B thus 
not releasing the python global lock, so thread A can't get it any more.

But I can now start thread B over and over again and it runs w/o problems 
while thread A still hangs until it's killed.

So if I understood things right, it's very unlikely the global lock is held 
by someone, otherwise thread B won't be able to run also.

So... Has anyone any idea what the reason for this problem could be? Or 
experienced anything like this? Or any insights in Python / pthreads that 
could help?

I'm appreciating any suggestions on how to find the culprit. 

BTW: This is Python 2.2.1. Have browsed through the WhatsNew of 2.2.2 but 
couldn't find any signs of a multithreaded problem fixed there so I didn't 
try it so far...



More information about the Python-list mailing list