# Re-Occurrence of bug #199 in code based on gr\_bin\_statistics\_f

**URL:** <https://www.ruby-forum.com/t/re-occurrence-of-bug-199-in-code-based-on-gr-bin-statistics-f/202891>\
**Category:** GNU Radio\
**Created:** [February 8, 2011, 3:07pm UTC](https://www.ruby-forum.com/t/re-occurrence-of-bug-199-in-code-based-on-gr-bin-statistics-f/202891 "2011-02-08T15:07:31Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [February 8, 2011, 3:07pm UTC](https://www.ruby-forum.com/t/re-occurrence-of-bug-199-in-code-based-on-gr-bin-statistics-f/202891/1 "2011-02-08T15:07:31Z")

</div>

Hello all,

It looks as though I have uncovered GNU Radio bug #199 in one of my  
programs. I used gr\_bin\_statistics\_f as a template and designed a new  
block  
which uses the same method to callback to Python code and retune the  
USRP.

[http://gnuradio.org/redmine/issues/show/199](http://gnuradio.org/redmine/issues/show/199)

Now, I could get into more details of my specific program, however it  
seems  
like this bug could be hard to replicate on different hardware. I am not  
sure to the extent of the issue, but I know where the problem is.

* * *

The problem occurs in the callback code in gr\_feval.i. As described in  
the  
bug report, Py\_GILState\_Ensure() is called before calleval(), and then  
Py\_GILState\_Release() is called after returning from Python land. It is  
at  
this point that I get a segmentation fault.

For a gdb stacktrace similar to that on the bug report, please refer to  
my  
blog post:

> **[GNU Radio bug #199](http://tomonprogramming.blogspot.com/2011/02/gnu-radio-bug-199.html)**
>
> Update: After much reading and testing, I simply added a call to PyEval\_InitThreads() which got rid of the segmentation fault. As a bonus,...

After googling around for a while I read about similar issues which were  
solved by adding a call to PyEval\_InitThreads() right before  
Py\_GILState\_Ensure() is called (hinted at in the bug report). This did  
the  
trick for me! Calling PyEval\_InitThreads() acquires and initializes the  
GIL,  
and calling it multiple times is a NOOP. More details can be found in  
Python  
documentation. I am not sure if this is the correct solution, but it  
worked  
for me. Others have suggested to me that the module using the  
multi-threaded  
Python extension should ‘import threading’ early to ensure that  
PyEval\_InitThreads is called.

Finally, here is the system I tested on:  
Fedora 14 64-bit  
Linux tjadesktop 2.6.35.10-72.fc14.x86\_64 #1 SMP Mon Dec 20 21:14:22 UTC  
2010 x86\_64 x86\_64 x86\_64 GNU/Linux  
Intel i5 760  
USRP 1 w/ WBX d’board

I have attached a small patch file which includes my fix. I could submit  
a  
formal fix if that would help, but I would really just like to get some  
feedback on the issue. What do you guys think?

Thanks!  
-Tom
