KeyboardInterrupt not being caught in top_block

Basically, I want to catch Ctrl+C after top_block.run()
For example

try:

    my_top_block().run()

except KeyboardInterrupt:

    print "Ctrl+C has been pressed. Exiting."

However, when Ctrl+C is pressed, the program exits straight away without
executing the print function.

It seems that KeyboardInterrupt is not propagated to the main thread?

What am I doing wrong?

By the way, if I caught some exception in work(), what is the best way
to
exit the program? top_block.stop() or raise SystemExit, 1?

Regards

Kyle

What version of GNU Radio?
What OS, distribution, version?
(We fixed bugs in this area about a year ago…)

On Thu, Jun 17, 2010 at 12:29:10AM +1000, Kyle Z. wrote:

    print "Ctrl+C has been pressed. Exiting."

By the way, if I caught some exception in work(), what is the best way to
exit the program? top_block.stop() or raise SystemExit, 1?

Just return -1 from work. The graph will stop.

Eric

On Wed, Jun 16, 2010 at 7:29 AM, Kyle Z. [email protected] wrote:

However, when Ctrl+C is pressed, the program exits straight away without
executing the print function.

It seems that KeyboardInterrupt is not propagated to the main thread?

What am I doing wrong?

The “except KeyboardInterrupt” catches anything typed on the keyboard as
a
normal character. This interrupt is thrown so that the OS can capture
the
input from the keyboard no matter what else is going on, and that the
data
being sent to the computer is not lost.

The Ctrl+C creates a special command generated by the OS called SIGINT.
The
default way a process handles this signal is to kill itself. In order
to
have your own process handle this kind of interrupt, you need to need to
create a specific method to capture SIGINT and handle it in your own
way.

Attached is a snippet of code from Stack Overflow (
controls - How do I capture SIGINT in Python? - Stack Overflow)
that shows how to do what you need.

-Michael B.

Thanks Eric,
I am using GNU Radio git depository installed a month ago.
tried on Cygwin 1.7.5+WinXP and OSX 10.6.3+Macbook, both behaved the
same.
I’ll try ubuntu tomorrow when I have access to another PC.
Ctrl+C exits the program but not caught by except.

On Thu, Jun 17, 2010 at 02:53:28PM +1000, Kyle Z. wrote:

Thanks Eric,
I am using GNU Radio git depository installed a month ago.
tried on Cygwin 1.7.5+WinXP and OSX 10.6.3+Macbook, both behaved the same.
I’ll try ubuntu tomorrow when I have access to another PC.
Ctrl+C exits the program but not caught by except.

Kyle,

Are you trying to set a handler for a signal somewhere in your code?
If so, it’s unlikely to work. In general signals and threads don’t
play together well. A bit of googling will show you the mine field.

If you’re trying to catch a std::exception, that should work fine.

Eric

On 18/06/2010, at 2:56 AM, Eric B. wrote:

Kyle,

Are you trying to set a handler for a signal somewhere in your code?
If so, it’s unlikely to work. In general signals and threads don’t
play together well. A bit of googling will show you the mine field.

If you’re trying to catch a std::exception, that should work fine.

Eric

Hi Eric
I did not set any handler except ‘except KeyboardInterrupt: …’
I tried it on Ubuntu as well which turned out to fail.
Yes, I agree with you that it’s likely the problem of coworking of
signals and threads.
I tried a single threaded program where the same mechanism did work.
So I will change my approach completely.
Thanks for making things clarified.
Kyle

On Fri, Jun 18, 2010 at 06:14:55PM +1000, Kyle Z. wrote:

Eric

Hi Eric
I did not set any handler except ‘except KeyboardInterrupt: …’
I tried it on Ubuntu as well which turned out to fail.
Yes, I agree with you that it’s likely the problem of coworking of signals and threads.
I tried a single threaded program where the same mechanism did work.
So I will change my approach completely.
Thanks for making things clarified.
Kyle

Kyle,

I just spent a few minutes reviewing the guts of the code. We do all
the magic to detect ^C in python, and then just tell the flow graph to
stop cleanly and wait for it to finish. We do not propagate the
python exception since it’s often times caught “in the wrong thread”.

Summary: the built in behavior is to map ^C (SIGINT) into code that
stops the graph. That code is very tricky, took a long time to get
working reliably, and I suggest you don’t mess with it. However, if
you’re curious, see gnuradio-core/src/python/gnuradio/gr/top_block.py
and gnuradio-core/src/lib/runtime/gr_top_block.i

Eric