KeyboardInterrupt and threading

Michael Hudson mwh at python.net
Wed Jan 7 07:28:32 EST 2004


q2n8byu02 at sneakemail.com (Ivan Nestlerode) writes:

> Michael Hudson <mwh at python.net> wrote in message news:<m3fzeu73k3.fsf at pc150.maths.bris.ac.uk>...
> > On debian, the signal module certainly should be available, and the
> > KeyboardInterrupt exception should go to the main thread.  Or at
> > least, that's what I thought.  You're definitely seeing it being
> > delivered to an arbitrary thread?
> > 
> > Cheers,
> > mwh
> 
> After a closer look, I am not seeing KeyboardInterrupt being delivered
> to non-main threads. What I am seeing is that Ctrl-C does not work at
> all when non-main threads are CPU intensive. 

Oh...

> I had previously jumped to the conclusion that because Ctrl-C didn't
> work and because the non-main thread was sloppy ("except:"), that
> Ctrl-C wasn't working because KeyboardInterrupt was going to the
> sub-thread. That was an incorrect conclusion.

Well, in a way, good, but perhaps not for you...

> So I guess the entire problem can be restated as: How do I make
> Ctrl-C work properly when the non-main thread is busier than the
> main thread?

This is hard for me to answer -- it works fine for me.  What platform
are you on?  ISTR Debian, but not versions of anything.

I'm somewhat inclined to start blaming libc at this point...

As you may well be aware, the combination of threads and signals is a
bit of a minefield.  Python runs all threads other than the main
thread with all signals masked. My understanding is that when a signal
is delivered to a process it's delivered to an arbitrary thread that
has that signal unmasked -- in Python's case, this should be the main
thread.  It kind of sounds like in your case the signal is being
queued up on a non-main thread, but as the signal is never unblocked,
it never gets handled.

[...]
> I think this is a bug though (I shouldn't have to put hacks into the
> main thread to get this to work).

Agreed, but I'm not sure they're in Python.

> Any thoughts?

Upgrade glibc?

Cheers,
mwh

-- 
     ARTHUR:  Why are there three of you?
  LINTILLAS:  Why is there only one of you?
     ARTHUR:  Er... Could I have notice of that question?
                   -- The Hitch-Hikers Guide to the Galaxy, Episode 11



More information about the Python-list mailing list