Now we figure out what is really happening of the bug:
Before we look into the code, we should know two things:
When we define a __getattr__
method for our class, we should never try to get an attribute that does not belong to the class or the instance itself in __getattr__
, otherwise it will cause infinite loop, for example:
class TestObject:
def __getattr__(self, item):
return self.a
if __name__ == "__main__":
testobject = TestObject()
print(f"print a: {testobject.a}")
The result should be like this:
Traceback (most recent call last):
File "tmp_test.py", line 10, in <module>
print(f"print a: {testobject.a}")
File "tmp_test.py", line 6, in __getattr__
return self.a
File "tmp_test.py", line 6, in __getattr__
return self.a
File "tmp_test.py", line 6, in __getattr__
return self.a
[Previous line repeated 996 more times]
RecursionError: maximum recursion depth exceeded
Cause a
is not in the instance's __dict__
, so every time it can not find a
, it will go into the __getattr__
method and then cause the infinite loop.
The next thing we should remember is how the pickle
module in python
works. When pickling and unpickling one class's instance, its dumps
and loads
(same for dump
and load
) function will call the instance's __getstate__
(for dumps
) and __setstate__
(for loads
) methods. And guess when our class does not define these two methods, where python will look for? Yes, the __getattr__
method! Normally, it is ok when pickling the instance, cause for this time, the attributes used in __getattr__
still exist in the instance. But when unpickling, things go wrong.
This is how the pickle
module documentation says when pickling the class's instance: https://docs.python.org/3/library/pickle.html#pickling-class-instances.
And here is what we should notice:

It means when unpickling one class's instance, it will not call the __init__
function to create the instance! So when unpickling, pickle's loads
function would check whether the re-instantiate instance has the __setstate__
method, and as we said above, it will go into the __getattr__
method, but for now, the attributes that the instance once owned has not been given (at the code obj.__dict__.update(attributes)
), so bingo, the infinite loop bug appears!
To reproduce the whole exact bug, you can run this code:
import pickle
class TestClass:
def __init__(self):
self.w = 1
class Test:
def __init__(self):
self.a = TestClass()
def __getattr__(self, item):
print(f"{item} begin.")
print(self.a)
print(f"{item} end.")
try:
return self.a.__getattribute__(item)
except AttributeError as e:
raise e
# def __getstate__(self):
# return self.__dict__
#
# def __setstate__(self, state):
# self.__dict__ = state
if __name__ == "__main__":
test = Test()
print(test.w)
test_data = pickle.dumps(test)
new_test = pickle.loads(test_data)
print(new_test.w)
You should get the infinite bug when not add the __getstate__
and __setstate__
method, and add them will fix it. You can also try to see the print info
to see whether the bug exists at __getattr__('__setstate__')
.
And the connection between this pickle
bug and our multiprocessing
bug at beginning is that it seems when using `spawn``, the son process's context would try to pickle the father process's context and then unpickle it and inherit it. So now all things make sense.