atexit + threads = bug?

Tim Peters tim.peters at gmail.com
Thu Jan 12 15:50:55 EST 2006


[David Rushby]
> ...
> I understand your explanation and can live with the consequences, but
> the atexit docs sure don't prepare the reader for this.

In fact, they don't mention threading.py at all.

> They say, "Functions thus registered are automatically executed upon
> normal interpreter termination."  It seems like sophistry to argue that
> "normal interpreter termination" has occurred when there are still
> threads other than the main thread running.

Well, since atexit callbacks are written in Python, it's absurd on the
face of it to imagine that they run after the interpreter has torn
itself down.  Clearly Python is still running at that point, or they
wouldn't get run at all.

It's also strained to imagine that threads have nothing to do with
shutdown, since the threading docs say "the entire Python program
exits when only daemon threads are left".  It's not magic that
prevents Python from exiting when non-daemon threads are still
running.  You happened to use the same non-magical hack that
threading.py uses to fulfill that promise, and you're seeing
consequences of their interaction.  In Python as well as in C, atexit
only works well when it's got exactly zero or one users <0.1 wink>.

You're welcome to suggest text you'd like better, but microscopic
examination of details most people will never care about makes for bad
docs in a different way.  To get a full picture of how CPython's
shutdown works, you need to explain all of Py_Finalize() in English,
and you need to get agreement on which details are accidents and which
are guaranteed.

Now it's probably a fact that you couldn't care less about 99.9% of
those finalization details:  you only care about the one that just bit
you.  How are you going to beef up the docs in such a way that you
would have _found_ the bit you cared about, among the vast bulk of new
detail you don't care about?

You aren't, so you could settle for suggesting new words that just
cover the bit you care about.  Give it a try!

> Suppose that today I promise to donate my body to science "upon my
> death", and tomorrow, I'm diagnosed with a gradual but inexorable
> illness that will kill me within ten years.  I wouldn't expect to be
> strapped down and dissected immediately after hearing the diagnosis, on
> the basis that the mere prophecy of my death is tantamount to the death
> itself.

Next time, quit while you're ahead ;-)



More information about the Python-list mailing list