Recursive logging crashes Interpreter in Python 3

。_饼干妹妹 提交于 2021-02-07 17:22:19

问题


Following code logs an error and calls itself leading to stack overflow and eventually core dump in Python 3.6.

>>> import logging
>>> def rec():
...     logging.error("foo")
...     rec()
>>> rec()

[1]    101641 abort (core dumped)  python3

FTR, this doesn't crash Python 2.7.

Attaching the error (condensed) in Python 3.6:

ERROR:root:foo
...
--- Logging error ---
Traceback (most recent call last):
...
RecursionError: maximum recursion depth exceeded in comparison
...
Fatal Python error: Cannot recover from stack overflow.
...
[1]    101641 abort (core dumped)  python3

Python 2.7:

RuntimeError: maximum recursion depth exceeded

But no core dump in Python 2.7.

FTR, the error above with Python 3.6 will come into play if the log level is set to logging.ERROR. Similarly for other log levels.

UPDATE: I have logged issue to follow this up with the Python community.


回答1:


Your code will eventually result in a RuntimeError (python 2.7) or RecursionError (python 3.6). This is because the recursion depth in both is limited to 1000:

>>>import sys
>>>sys.getrecursionlimit()
1000

If it's actually causing a stack-overflow induced crash, one possible reason is that the recursion depth limit has been modified.



来源:https://stackoverflow.com/questions/55116805/recursive-logging-crashes-interpreter-in-python-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!