[Python-Dev] Status of thread cancellation

"Martin v. Löwis" martin at v.loewis.de
Thu Mar 15 17:24:31 CET 2007


Jean-Paul Calderone schrieb:
>> I inferred from Martin's proposal that he
>> expected the thread to be able to catch the exception.  Perhaps he can
>> elaborate on what cleanup actions the dying thread will be allowed to
>> perform.
> 
> Perhaps he can.  Hopefully, he can specifically address these points:
> 
>    1. A thread can throw a ThreadDeath exception almost anywhere. All
>       synchronized methods and blocks would have to be studied in great
>       detail, with this in mind.
> 
>    2. A thread can throw a second ThreadDeath exception while cleaning up
>       from the first (in the catch or finally clause). Cleanup would have
>       to repeated till it succeeded. The code to ensure this would be quite
>       complex. 

Clearly, a thread need to have its finally blocks performed in response
to a cancellation request. These issues are real, however, they apply
to any asynchronous exception, not just to thread cancellation.

In Python, we already have an asynchronous exception: KeyboardInterrupt.
This suffers from the same problems: a KeyboadInterrupt also can occur
at any point, interrupting code in the middle of its finally-blocks.
The other exception that is nearly-asynchronous is OutOfMemoryError,
which can occur at nearly any point (but of course, never occurs in
practice).

So yes, it would be good if Python's exception handling supported
asynchronous exceptions in a sensible way. I have to research somewhat
more, but I think the standard solution to the problem in operating
system (i.e. disabling interrupts at certain points, explicitly
due to code or implicitly as a result of entering the interrupt
handler) may apply.

Regards,
Martin


More information about the Python-Dev mailing list