递归日志记录崩溃了Python 3中的Interpreter

问题描述 投票:4回答:1

以下代码记录错误并调用自身导致堆栈溢出并最终导致Python 3.6中的核心转储。

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

[1]    101641 abort (core dumped)  python3

FTR,这不会让Python 2.7崩溃。

在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

但是在Python 2.7中没有核心转储。

FTR,如果日志级别设置为logging.ERROR,则Python 3.6上面的错误将起作用。其他日志级别也是如此。

更新:我已经记录了issue以跟进Python社区。

python python-3.x logging python-3.6 cpython
1个回答
2
投票

你的代码最终将导致RuntimeError(python 2.7)或RecursionError(python 3.6)。这是因为两者中的递归深度限制为1000:

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

如果它实际上导致堆栈溢出引起的崩溃,一个可能的原因是已经修改了递归深度限制。

© www.soinside.com 2019 - 2024. All rights reserved.