[Python-bugs-list] [ python-Bugs-502236 ] Need way to exit application

noreply@sourceforge.net noreply@sourceforge.net
Fri, 11 Jan 2002 17:05:32 -0800


Bugs item #502236, was opened at 2002-01-11 00:46
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=502236&group_id=5470

Category: Extension Modules
Group: Feature Request
Status: Open
Resolution: None
Priority: 5
Submitted By: paul rubin (phr)
Assigned to: Nobody/Anonymous (nobody)
Summary: Need way to exit application

Initial Comment:
I may be missing something but there doesn't seem to
be any easy, correct way to exit a Python application
and shut down the interpreter.  sys.exit() raises
the SysExit exception in the current thread but other
threads keep running, so the application doesn't stop.
You can do something brutal like os.kill() your
process, but then cleanup actions (finally clauses)
don't get run.  Or you can create some elaborate
application-specific framework for propagating an
exit flag from one thread to all the rest.  That's
incredibly lame.  

What's needed is a simple function (maybe sys.allexit)
that raises SysExit in all the threads of the
application (it might have to first stop all the 
threads using the GIL or something like that).  

I'm surprised there isn't already something like this, 
but there's been some c.l.py discussion about it and 
it really seems to not be there.  Is there some 
problem with the idea?  I don't see one but I can't be 
sure.

----------------------------------------------------------------------

>Comment By: Martin v. Löwis (loewis)
Date: 2002-01-11 17:05

Message:
Logged In: YES 
user_id=21627

Tim, I'm not surprised that raising SystemExit in a thread
fails to exit Python; the reason simply is that
threadmodule.c:t_bootstrap choses to ignore
PyExc_SystemExit, when it instead should invoke
PyErr_PrintEx instead.

The bug is obviously that exit_thread also raises
SystemExit, when people apparently really want sys.exit and
thread.exit to be two different things. So I think
thread.exit should raise ThreadExit, and raising SystemExit
in a thread should cause Py_Exit to be invoked (along with
all other necessary cleanup). In short, 2.12 of
threadmodule.c was wrong, IMO.


----------------------------------------------------------------------

Comment By: Tim Peters (tim_one)
Date: 2002-01-11 13:16

Message:
Logged In: YES 
user_id=31435

Martin, change your program to do sys.exit(1) inside func() 
on the first iteration (or raise SystemExit similarly), and 
whether you're on Windows or Linux you should find that 
while the func() thread goes away, the main program keeps 
on running.  If you do os._exit(1) instead, on Windows the 
main program does go away, but Guido said it doesn't on 
Linux (which didn't surprise me, given Linux's conflation 
of threads with processes).  See also the "Killing Threads" 
thread in Python-Dev from late May of 2001, which said all 
the same things.

The only thing I may <wink> have learned since then is that 
supposedly the *C* exit() function terminates all threads 
on Linux, via an atexit handler installed by libc, and so 
_exit() on Linux doesn't terminate all threads because _exit
() doesn't call atexit functions.

----------------------------------------------------------------------

Comment By: Martin v. Löwis (loewis)
Date: 2002-01-11 08:19

Message:
Logged In: YES 
user_id=21627

Can you produce an example demonstrating the problem? Please
see the attached thr.py. It terminates fine; I fail to see
the problem.

When SystemExit is raised, it will eventually invoke the C
library's exit(). If that is not incredibly lame, it will
terminate all threads.

----------------------------------------------------------------------

Comment By: Guido van Rossum (gvanrossum)
Date: 2002-01-11 07:46

Message:
Logged In: YES 
user_id=6380

I'm struggling with this in a threaded distributed app we're
developing for Zope Corp, so I agree that something like
this would be nice. I think for starters, there should be a
way to raise an asynchronous exception in a specific thread;
all threads is easy then.

There are plenty of implementation problems with this,
however: if a thread is blocked in I/O, there's no portable
way to get it to bail out of that I/O operation.

There are also plenty of application-level issues: if
asynchronous exceptions are common, maybe it is necessary to
provide a way to temporarily block such exceptions in order
to provide safety in some situations.

Etc., etc. So please spare us your surprise and help looking
for a mechanism that can be implemented.

----------------------------------------------------------------------

You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=502236&group_id=5470