mongoengine icon indicating copy to clipboard operation
mongoengine copied to clipboard

Question regarding usage of weakref.proxy

Open gnulnx opened this issue 8 years ago • 2 comments

We have noticed a few "Weakref errors gone away" in a python3 code base. This error was not noticed in the python2 code base. I have traced it down to this line:

self._instance = weakref.proxy(instance)

https://github.com/MongoEngine/mongoengine/blob/master/mongoengine/base/datastructures.py#L25

If I simply remove the weakref.proxy(instance) and do the direct assignment (aka a hard references) then the code works as expected. I can't find anything in our code base that would make the reference disappear.

So my question to the team here is what was the intended functionality of the weakref vs a normal hard reference in this part of the code?

gnulnx avatar Aug 17 '18 13:08 gnulnx

Anyone have any thoughts on this?

gnulnx avatar Sep 20 '18 16:09 gnulnx

Hi @gnulnx, I'm not exactly sure but it might be related to garbage collection, since Document has a direct reference to its embedded documents and vice-versa (through _instance in the embedded structure), AFAIK garbage collector may not call destructors properly in case of circular references.

We are using mongoengine with python 3.6.5 on production for a while and I never saw that error. Is it reproducible on your end? If yes, please provide a few line that demonstrates the issue.

bagerard avatar Mar 17 '19 22:03 bagerard