[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 20:01:35 CEST 2009

Hi all,

I'm working on http://bugs.python.org/issue6642 for unladen swallow
(because it happens to bite us in a weird way), and Jeff Yasskin told
me to ask python-dev what the proper behavior should be at exit from
the child process of a fork from a spawned thread.

Right now, it seems that there is an assumption that when exiting the
process, control will always return through Py_Main, which in turn
calls Py_Finalize, to do things like GC and calling atexit handlers.
Normally, if you fork a process from the main thread, this assumption
will remain true, because main and Py_Main are still at the bottom of
the stack in the child process.  However, if you fork from a spawned
thread, then the root of the stack will be the thread bootstrap
routine for the platform's pythreads implementation.

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?


More information about the Python-Dev mailing list