This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author vstinner
Recipients Charles Bouchard-Légaré, JanVok, Peer Sommerlund, Sandeep, methane, r.david.murray, serhiy.storchaka, smurfix, veky, vinay.sajip, vstinner, yselivanov
Date 2020-11-02.22:32:13
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1604356334.49.0.22094073465.issue26789@roundup.psfhosted.org>
In-reply-to
Content
I fixed the issue in the master and I will not backport the change to stable branches on purpose. Thanks for the review Vinay!

--

I pushed a change in Python 3.10 which reduces the risk of "NameError: name 'open' is not defined" when logging really late during the Python finalization.

It's a workaround on fragile finalization code. Please don't rely on it! You should release resources explicitly at exit. Don't rely on implicit __del__() finalizers.

I chose to enhance the logging module anyway since the initial issue was that asyncio failed to log a message at exit, a message which is supposed to help developer to fix their code. It's a convenient tool to ease debugging. But, again, it would be really bad idea to rely on it to ensure that logging still works after logging.shutdown() has been called!

About asyncio, for development, you can try to frequently call gc.collect() using call_later() or something, to get issues like "never-retrieved exceptions":
https://docs.python.org/dev/library/asyncio-dev.html

I chose to restrict this issue to the very specific case of NameError when calling logging.FileHandler.emit() late during the Python finalization.

If someone wants to enhance another function, please open a new issue.

I will not backport my change since the Python finalization is really fragile and my logging fix rely on many enhancements made in the master that will not be backported to Python 3.9 and older on purpose. Changes on the finalization are also fragile and can introduce new subtile bugs. It happened multiple times. But overall, step by step, the Python finalization becomes more and more reliable ;-)

For curious people, here are my notes on the Python finalization:
https://pythondev.readthedocs.io/finalization.html

--

About the logging, honestly, I'm not sure that it's a good idea that FileHandler.emit() reopens the file after logging.shutdown() has been called (this function calls FileHandler.close() which closes the file).

But if someone disagrees, please open a separated issue ;-) This one is now closed.
History
Date User Action Args
2020-11-02 22:32:14vstinnersetrecipients: + vstinner, vinay.sajip, smurfix, r.david.murray, methane, serhiy.storchaka, yselivanov, veky, Peer Sommerlund, JanVok, Charles Bouchard-Légaré, Sandeep
2020-11-02 22:32:14vstinnersetmessageid: <1604356334.49.0.22094073465.issue26789@roundup.psfhosted.org>
2020-11-02 22:32:14vstinnerlinkissue26789 messages
2020-11-02 22:32:14vstinnercreate