New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Infinite recursion while garbage collecting loops indefinitely #55003
Comments
Hi While working on some Python code I stumbled on a situation where the Python process seems to hang indefinitely. Further debugging points to the following conclusion: if there is a class that somehow manages to run into an infinite recursion (properly detected by Python which raises the corresponding RuntimeError) while Python is doing garbage collection (so the infinite recursion has to be triggered from __del__ somehow) then Python hangs into an infinite loop apparently retrying to call that __del__ indefinitely. The described behavior happens ONLY when an infinite recursion is detected (ie if I raise my own "RuntimeError" from __del__ then it just logs it and exits). Program showing the problem: class A(object):
def __init__(self):
raise Exception('init error')
self.m = 'Hello world'
def __del__(self):
#raise RuntimeError('my runtime error')
self.__del__()
def func():
h = A()
func() $ python pyloop.py Traceback (most recent call last):
File "pyloop.py", line 15, in <module>
func()
File "pyloop.py", line 13, in func
h = A()
File "pyloop.py", line 5, in __init__
raise Exception('init error')
Exception: init error
Exception RuntimeError: 'maximum recursion depth exceeded' in <bound method A.__del__ of <__main__.A object at 0x17bb7d0>> ignored
Exception RuntimeError: 'maximum recursion depth exceeded' in <bound method A.__del__ of <__main__.A object at 0x17bb7d0>> ignored
Exception RuntimeError: 'maximum recursion depth exceeded' in <bound method A.__del__ of <__main__.A object at 0x17bb7d0>> ignored
Exception RuntimeError: 'maximum recursion depth exceeded' in <bound method A.__del__ of <__main__.A object at 0x17bb7d0>> ignored
... If I uncomment the line raising my RuntimeError instance from __del__ then the exception is logged once and the program exits (as expected). Please help, thanks. |
Normally you should never call __del__, OTOH the issue is the same with a class like:: class A:
def close(self):
self.close()
def __del__(self):
self.close() The problem is not with _infinite_ recursion, though; a depth of 47 is enough (but 46 won't show the problem):: class A:
def __del__(self):
self.recurse(47)
def recurse(self, n):
if n:
self.recurse(n-1)
else:
raise ValueError Hint: PyTrash_UNWIND_LEVEL is defined to 50; I checked that recompiling Python with a different value also changes the necessary recursion depth. There is some nasty interaction between the "Trashcan mechanism" and resurrected objects stored in deep structures. A list is enough, no need to involve frames and exceptions:: class C:
def __del__(self):
print ".",
x = self
for i in range(49): # PyTrash_UNWIND_LEVEL-1
x = [x]
l = [C()]
del l |
Precision: with new-styles classes (or py3k) the limit is PyTrash_UNWIND_LEVEL-2. This does not change anything to the problem. |
FWIW, the example pasted in the bug was the smallest one he could come up |
2.6 is finished except for possible security patches. |
it happens on 3.2 (py3k head). |
I think this issue is out of date. For Mihai's example I get: >>> class A(object):
... def __init__(self):
... raise Exception('init error')
... self.m = 'Hello world'
... def __del__(self):
... #raise RuntimeError('my runtime error')
... self.__del__()
...
>>> def func():
... h = A()
...
>>> func()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in func
File "<stdin>", line 3, in __init__
Exception: init error
>>> For Amaury's first example I get what I expect: >>> class A:
... def close(self):
... self.close()
... def __del__(self):
... self.close()
...
>>> class A:
... def close(self):
... self.close()
... def __del__(self):
... self.close()
...
>>> def func():
... h = A()
...
>>> func()
Exception ignored in: <function A.__del__ at 0x000002921A8E1820>
Traceback (most recent call last):
File "<stdin>", line 5, in __del__
File "<stdin>", line 3, in close
File "<stdin>", line 3, in close
File "<stdin>", line 3, in close
[Previous line repeated 994 more times]
RecursionError: maximum recursion depth exceeded
>>> And for Amaury's trashcan example (even when I increase the list length to well over the trashcan threshold): >>> class C:
... def __del__(self):
... print('.')
... x = self
... for i in range(49): # PyTrash_UNWIND_LEVEL-1
... x = [x]
...
>>> l = [C()]
>>> del l
.
>>> class C:
... def __del__(self):
... print('.')
... x = self
... for i in range(5000):
... x = [x]
...
>>> l = [C()]
>>> del l
.
>>> |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: