[Python-Dev] Where to put the interrupt module?

Just van Rossum just@letterror.com
Fri, 13 Jun 2003 14:26:52 +0200


Kevin Jacobs wrote:

> > > Ah.. that sounds familiar.  I knew there was a reason I didn't
> > > pursue this avenue farther last time I poked through that code.
> > 
> > Same here. The code that checks for interrupts explicitly does
> > nothing when called from a thread that's not the main thread.
> 
> That chunk of code is very problematic.  Some platforms/threading
> libraries will deliver a SIGINT to all threads, which can abort many
> system calls that are not trivially restarted/restartable.  To
> deliver a KeyboardInterrupt only to the main thread is problematic,
> since the other threads may misinterpret the failure as something
> else (EOF, fatal error, or worse, operation succeeded).
> 
> I understand the desire to smooth over platform differences in
> Python, but I'm not sure that this one (signal delivery policy to
> threads) is a good idea.  In fact, I'll try to find a real-world case
> where this kind of error occurs.

While the signal module is used in the implementation of
PyErr_SetInterrupt(), I don't think signals play a part when
interrupting threads: all that needs to be done is add a pending call
that sets KeyboardInterrupt and returns -1.

The problem is that the pending calls mechanism is for the main thread
onle, and involves globals (mainly ceval.c's 'pendingcalls' and
'things_to_do'). So the first thing we'd need to look into is moving
those globals to the PyThreadState struct (or to PyThreadState's dict
object).

(Btw. whatever we do, it won't interrupt blocking system calls, but I
think that's fine. sys.interrupt() won't interrupt these either.)

Just