[Python-Dev] Threading, atexit, and logging

"Martin v. Löwis" martin at v.loewis.de
Wed Dec 6 08:37:12 CET 2006


In bug #1566280 somebody reported that he gets an
exception where the logging module tries to write
to closed file descriptor.

Upon investigation, it turns out that the file descriptor
is closed because the logging atexit handler is invoked.
This is surprising, as the program is far from exiting at
this point.

Investigating further, I found that the logging atexit
handler is registered *after* the threading atexit handler,
so it gets invoked *before* the threading's atexit.

Now, threading's atexit is the one that keeps the
application running, by waiting for all non-daemon threads
to shut down. As this application does all its work in
non-daemon threads, it keeps running for quite a while -
except that the logging module gives errors.

The real problem here is that atexit handlers are
invoked even though the program logically doesn't exit,
yet (it's not just that the threading atexit is invoked
after logging atexit - this could happen to any atexit
handler that gets registered).

I added a patch to this report which makes the MainThread
__exitfunc a sys.exitfunc, chaining what is there already.
This will work fine for atexit (as atexit is still imported
explicitly to register its sys.exitfunc), but it might break
if other applications still insist on installing a
sys.exitfunc.

What do you think about this approach?

Regards,
Martin

P.S. There is another issue reported about the interpreter
crashing; I haven't been able to reproduce this, yet.



More information about the Python-Dev mailing list