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

Reid Kleckner rnk at mit.edu
Tue Sep 1 21:05:04 CEST 2009


On Tue, Sep 1, 2009 at 2:58 PM, "Martin v. Löwis"<martin at v.loewis.de> wrote:
>> On one hand, you may not want to call the user's atexit handlers
>> multiple times from different processes if they have externally
>> visible effects.  On the other hand, people seem to assume that
>> Py_Finalize will be called at process exit to do various cleanups.  On
>> the third hand, maybe Python could just clear out all the atexit
>> handlers in the child after a fork.  So what should the correct
>> behavior be?
>
> 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.  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.

Reid


More information about the Python-Dev mailing list