How to savely end a python sub-interpreter from a different thread?
Gernot Hillier
ghillie at suse.de
Wed Nov 20 03:41:38 EST 2002
Hi!
I'm developing a communication application which uses python as embedded
script language. The python controls the work which is done when the
connection is established. So when I receive a connection request, a new
thread containing a new python sub-interpreter is created.
Now I have the problem that the connection can be ended at any time from the
other side. I get a notification about this in another thread.
Now my problem is: how to end the python script gracefully at this moment?
Currently I've introduced a new Exception in my python extension. I then
throw this exception from the other thread into the right python
interpreter. So the script will (hopefully) get it and can end gracefully.
Here's some code of the other thread:
void Connection::ConnectionLost()
{
[...]
if (py_state) {
// acquire lock, switch to right thread context
PyEval_RestoreThread(py_state);
PyErr_SetString(CallGoneError,"Call was finished from partner.");
PyEval_SaveThread();
}
[...]
}
py_state is set to the PyThreadState* of the running Python interpreter.
This works most times, but sometimes it seems to cause some problems leading
to crashes in Py_EndInterpreter() for the current connection or the
subsequent Py_NewInterpreter() for the next connection. These problems
especially seem to occur when two Python sub-interpreters are deleted
nearly at the same time.
As I suspected stack corruption, I tried to LD_PRELOAD libefence.so. Now I
get the following back trace:
Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 1026 (LWP 12720)]
0x080ce3e3 in PyErr_SetObject (exception=0x432a8fe0, value=0x42152fc8) at
Python/errors.c:55
55 Py_XINCREF(exception);
Current language: auto; currently c
(gdb) bt
#0 0x080ce3e3 in PyErr_SetObject (exception=0x432a8fe0, value=0x42152fc8)
at Python/errors.c:55
#1 0x080ce4ae in PyErr_SetString (exception=0x432a8fe0, string=0x8103420
"Connection was finished from partner.") at Python/errors.c:70
#2 0x0805d96d in Connection::ConnectionLost() () at connection.cpp:351
Can someone tell me what's the problem here?
Or has someone any better idea what to do to end the python interpreter?
TIA!!
--
Ciao,
Gernot
More information about the Python-list
mailing list