classification
Title: Kill threads from main thread
Type: enhancement Stage:
Components: Extension Modules Versions:
process
Status: closed Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: gvanrossum, holtwick
Priority: normal Keywords:

Created on 2000-11-02 15:51 by holtwick, last changed 2000-11-13 20:21 by gvanrossum. This issue is now closed.

Messages (7)
msg2301 - (view) Author: Dirk Holtwick (holtwick) Date: 2000-11-02 15:51
It still isn't possible to kill a thread from another thread. This is an important feature for long running processes as multithreaded application servers. It should be possible for the main thread (or another thread) to kill a non responding thread after a timeout.

As I read in a discussion forum "pthreads" support somewhat like thread_cancel() for Windows I don't know.

If this feature became implemented it would be nice if an ThreadObject from the module "threading" could be killed by deleting the object.
msg2302 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2000-11-02 17:31
This is a very controversial feature. If a thread owns a resource (e.g. it holds a lock), what happens to the resource when the thread is killed?

The best idea I can come up with is to make it possible to send asynchronous exceptions to threads -- but this doesn't help for threads that are blocked in an I/O operation. That might be solved through sending signals, but that's a Unix-only solution, and probably isn't even consistent across different Unix thread implementations.
msg2303 - (view) Author: Dirk Holtwick (holtwick) Date: 2000-11-02 18:49
Blocking I/O operations are a problem, but what about other internal problems like endless loops? An asynchronous exception would be a good way for most problems (I have with threads ;) And as far as the documentation says, signals always go to the main thread. If that is so, it doesn't help much. 

So what about connecting signals to threads. I don't know if this is possible, but a reliable way to use signals with threads would be a way to give threads a timeout.
msg2304 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2000-11-02 20:04
As I said, signals are a Unix-only solution.

And blocked I/O is a common reason to *want* to kill a thread -- e.g. the common problem of webservers that simply never reply.
msg2305 - (view) Author: Dirk Holtwick (holtwick) Date: 2000-11-03 13:01
I think then it would be the best to send the asynchronous exceptions even if they don't work for blocking I/O operations. The need of killing threads from outside is obvious I think. And there will be for sure some scenarios where the killing doesn't work properly, but to avoid these situations should be a task for the programmer of the code. There are always situations where the design of a programming language will not save the programmer of writing stupid code, e.g. while 1: pass

If there is no way of implementing the killing how should one write an multithreaded long running programm that keeps itself clean for several days or months or years. The advantage of threads are the shared informations and forking is often more difficult to use.

How does Java solve these problems?
msg2306 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2000-11-03 13:18
The common solution for things like this is to have a simple app-specific protocol involving. The thread watches a specific variable that means "please exit"; when there's a need to kill the thread this variable is set. The thread just has to be careful to check this variable in each unbounded loop. This requires a bit of discipline; but (as you say) there are too many things a programmer can do to break the program to try and prevent them all.
msg2307 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2000-11-13 20:21
I've added this feature request to PEP 42.
History
Date User Action Args
2000-11-02 15:51:51holtwickcreate