Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: segmentation fault when executing PyImport_ImportModule("sys")

Thread Tools

Re: segmentation fault when executing PyImport_ImportModule("sys")

Dietrich Bollmann
Posts: n/a

I found the reason for the segmentation fault and hope that my solution
might be helpful for somebody else some day

On Tue, 2008-04-08 at 19:31 +0900, Dietrich Bollmann wrote:
Program received signal SIGSEGV, Segmentation fault.
> [Switching to Thread 0xb046ab90 (LWP 9854)]
> threadstate_getframe (self=0xb7e8a889) at ../Python/pystate.c:154
> 154 ../Python/pystate.c: No such file or directory.
> in ../Python/pystate.c
> (gdb) bt full
> #0 threadstate_getframe (self=0xb7e8a889) at ../Python/pystate.c:154
> No locals.
> #1 0xb7e8a897 in PyEval_GetGlobals () at ../Python/ceval.c:3340%G%

> ...snip...
> #3 0xb7eaede5 in PyImport_ImportModule (name=0x901504b "sys")%G%@
> ...snip...

The segmentation fault was caused by the interaction of my own code
with some changes made to the code base recently which where intended
to make the python functions thread save:

Here come the new additions to the function which starts the Python

void BPY_start_python( int argc, char **argv )
PyThreadState *py_tstate = NULL;


/* Initialize thread support (also acquires lock) */

/* Don'\''t allow the Python Interpreter to release the GIL on
* its own, to guarantee PyNodes work properly. For Blender this
* is currently the best default behavior.
* The following code in C is equivalent in Python to:
* "import sys; sys.setcheckinterval(sys.maxint)" */
_Py_CheckInterval = PyInt_GetMax();


py_tstate = PyGILState_GetThisThreadState();


Until recently only one thread accessed python objects and there was
no need for any code making Python thread-save.

This didn't cause any problem with my own code as I only inserted
events in form of python code to execute into a thread save event
queue which was worked off by the only thread accessing python

With the new python thread code added I had to surround all my code
which accesses python objects with PyGILState_*() in order to solve
the problem:

PyGILState_STATE gstate;

/* aquire python thread */
gstate = PyGILState_Ensure();

...access python objects...

/* release python thread */

The PyGILState_*() functions ensure that a global Python thread lock
is held by the current thread, protecting the Python objects from
being accessed by other threads in the same time.

For more detailed explanations see the chapter "8.1 Thread State and
the Global Interpreter Lock" (
in the Python/C API Reference Manual.

Thanks for your help,


Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
segmentation fault when executing PyImport_ImportModule("sys") Dietrich Bollmann Python 0 04-08-2008 08:31 AM
segmentation fault when executing PyImport_ImportModule("sys") Dietrich Bollmann Python 0 04-08-2008 08:24 AM
Segmentation fault using Firefox 15.0.2 Keith Lee Firefox 3 04-29-2006 05:45 PM
Xerces XML Parser Segmentation fault with Java Pud XML 0 11-06-2003 05:07 PM
Intel Xeon + Linux + IBM sdk 1.3.1 - getting Segmentation fault Alex Hunsley Java 17 11-06-2003 12:12 AM