[Python-Dev] AtExit Functions

Guido van Rossum guido@python.org
Tue, 09 Jul 2002 10:15:45 -0400


> While working with mxTextTools 2.1.0b2, Mike Fletcher found that
> he gets a fatal error when the interpreter exits.
> 
> Some tracing indicates that the cause is the at exit function
> of mxTextTools which clears the cache of tag tables used by
> the Tagging Engine in mxTextTools.
> 
> If these tables include references to (callable) Python instances,
> Python can't properly clean them up when decref'ing them at
> AtExit time.
> 
> Would it be safe to simply move the call_dll_exitfunc()
> call just before the "clear threat" code in Py_Finalize() ?

You mean call_ll_exitfuncs(). :-)

I think you may be making a wrong use of Py_AtExit().  The docs state
(since 1998):

  Since Python's internal finallization will have completed before the
  cleanup function, no Python APIs should be called by *func*.

I don't think it's safe to move the call forward.  (I don't know which
line you are referring to with ``"clear threat" code'' so I don't know
how far back you want to move it, but I think the intention is very
clear that this should be done at the very last.)

You may want to use the atexit.py module instead to schedule your
module's cleanup action; these exit functions are called much earlier.

--Guido van Rossum (home page: http://www.python.org/~guido/)