[Python-Dev] Whether to call Py_Finalize when exiting from the child process of a fork from a spawned thread

"Martin v. Löwis" martin at v.loewis.de
Tue Sep 1 21:48:41 CEST 2009

>> Standard POSIX fork semantics should be a guidance. IIUC, termination
>> of the last thread is equivalent to calling exit(0) (although return
>> from main() still means that exit is invoked right away, and the return
>> value of main is the exit code - right?). Calling exit means to call
>> all exit handlers.
> It depends, there is also _exit, which exists solely for the purpose
> of working around exit handlers being called from a forked child
> process at exit.

I don't think so. There are other cases where you don't want to run
atexit handlers, eg. in a segfault signal handler. _exit has been
there "forever", I believe (of course, so did fork()).

>  Which semantics should Python have?  In my opinion,
> it is more obvious that the user's handlers would be called than not,
> so I agree with you.

POSIX says that ending the last thread is equivalent to exit(0),
and this is what Python should also do when the last thread ends.

This is independent of somebody calling _exit(), which should have
the very effect that calling _exit has (i.e. immediate process


More information about the Python-Dev mailing list