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 vstinner
Date 2020-10-30.14:57:17
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
If you apply attached logging.patch, Python crash using attached

$ echo|./python -i
python: Python/_warnings.c:872: setup_context: Assertion `globals != NULL' failed.
Abandon (core dumped)

Attached logging.patch is a fix for bpo-26789: logging.FileHandler keeps a reference to the builtin open() function.

Attached does crash because of an unclosed file. It happens after PyInterpreterState_Clear(), in finalize_interp_clear():

    /* Last explicit GC collection */

setup_context() of Python/_warings.c does crash because tstate->interp->sysdict is NULL at this point.

Moreover, _io.TextIOWrapper.__init__() fails to import the locale module (to call locale.getpreferredencoding(), since the encoding is None) and picks the "ascii" encoding.

Moreover, even if I work around all these issues, _warnings.showwarnings() logs "lost sys.stderr" into stderr because, again, tstate->interp->sysdict is NULL at this point (and so sys.stderr no longer exists).

It looks like a bug in finalize_interp_clear() which triggers a garbage collection, whereas Python is no longer usable.


The logging module does automatically close files at exit using:


But, a new file is opened by a logging handler after logging.shutdown() is called. Maybe another problem is that the logging module does not clear logger handlers, or prevent handlers to log more message, after shutdown() is called.
Date User Action Args
2020-10-30 14:57:17vstinnersetrecipients: + vstinner
2020-10-30 14:57:17vstinnersetmessageid: <>
2020-10-30 14:57:17vstinnerlinkissue42208 messages
2020-10-30 14:57:17vstinnercreate