[New-bugs-announce] [issue42208] Using logging or warnings during Python finalization does crash Python
STINNER Victor
report at bugs.python.org
Fri Oct 30 10:57:17 EDT 2020
New submission from STINNER Victor <vstinner at python.org>:
If you apply attached logging.patch, Python crash using attached crash.py:
$ echo|./python -i crash.py
>>>
>>>
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 crash.py does crash because of an unclosed file. It happens after PyInterpreterState_Clear(), in finalize_interp_clear():
/* Last explicit GC collection */
_PyGC_CollectNoFail();
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:
atexit.register(shutdown)
But crash.py, 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.
----------
components: Interpreter Core
files: logging.patch
keywords: patch
messages: 379974
nosy: vstinner
priority: normal
severity: normal
status: open
title: Using logging or warnings during Python finalization does crash Python
versions: Python 3.10
Added file: https://bugs.python.org/file49553/logging.patch
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue42208>
_______________________________________
More information about the New-bugs-announce
mailing list