1

I'm having this problem in my django project that I believe is related to caches. It appears once every 6 months or so and is "fixed" by restarting the app, only to come back some months later. Suddenly all views will fail with this exception.

TypeError: weak object has gone away

And it's not always the same object, every view fails with trying to access a different object. In this case it's accessing the user (with request.user.is_authenticated()) but other pages its with other objects.

I'm thinking there must be something wrong with the caches. Maybe its a problem in django even. I dont know. It just works fine until it doesnt and nobody seems to talk about this exception on the internet.

Here is a sample traceback:

Traceback (most recent call last):

  File "/home/rsaenz/virtualenvs/club/lib/python2.7/site-packages/django/core/handlers/base.py", line 88, in get_response
    response = middleware_method(request)

  File "/home/rsaenz/webapps/club/club/cms/socios/views.py", line 143, in process_request
    if not request.user.is_authenticated():

  File "/home/rsaenz/virtualenvs/club/lib/python2.7/site-packages/django/utils/functional.py", line 213, in inner
    self._setup()

  File "/home/rsaenz/virtualenvs/club/lib/python2.7/site-packages/django/utils/functional.py", line 298, in _setup
    self._wrapped = self._setupfunc()

  File "/home/rsaenz/virtualenvs/club/lib/python2.7/site-packages/django/contrib/auth/middleware.py", line 18, in <lambda>
    request.user = SimpleLazyObject(lambda: get_user(request))

  File "/home/rsaenz/virtualenvs/club/lib/python2.7/site-packages/django/contrib/auth/middleware.py", line 10, in get_user
    request._cached_user = auth.get_user(request)

  File "/home/rsaenz/virtualenvs/club/lib/python2.7/site-packages/django/contrib/auth/__init__.py", line 140, in get_user
    user_id = request.session[SESSION_KEY]

  File "/home/rsaenz/virtualenvs/club/lib/python2.7/site-packages/django/contrib/sessions/backends/base.py", line 47, in __getitem__
    return self._session[key]

  File "/home/rsaenz/virtualenvs/club/lib/python2.7/site-packages/django/contrib/sessions/backends/base.py", line 173, in _get_session
    self._session_cache = self.load()

  File "/home/rsaenz/virtualenvs/club/lib/python2.7/site-packages/django/contrib/sessions/backends/db.py", line 20, in load
    expire_date__gt=timezone.now()

  File "/home/rsaenz/virtualenvs/club/lib/python2.7/site-packages/django/db/models/manager.py", line 151, in get
    return self.get_queryset().get(*args, **kwargs)

  File "/home/rsaenz/virtualenvs/club/lib/python2.7/site-packages/django/db/models/query.py", line 301, in get
    clone = self.filter(*args, **kwargs)

  File "/home/rsaenz/virtualenvs/club/lib/python2.7/site-packages/django/db/models/query.py", line 593, in filter
    return self._filter_or_exclude(False, *args, **kwargs)

  File "/home/rsaenz/virtualenvs/club/lib/python2.7/site-packages/django/db/models/query.py", line 611, in _filter_or_exclude
    clone.query.add_q(Q(*args, **kwargs))

  File "/home/rsaenz/virtualenvs/club/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1204, in add_q
    clause = self._add_q(where_part, used_aliases)

  File "/home/rsaenz/virtualenvs/club/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1240, in _add_q
    current_negated=current_negated)

  File "/home/rsaenz/virtualenvs/club/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1131, in build_filter
    clause.add(constraint, AND)

  File "/home/rsaenz/virtualenvs/club/lib/python2.7/site-packages/django/utils/tree.py", line 104, in add
    data = self._prepare_data(data)

  File "/home/rsaenz/virtualenvs/club/lib/python2.7/site-packages/django/db/models/sql/where.py", line 61, in _prepare_data
    if is_iterator(value):

  File "/home/rsaenz/virtualenvs/club/lib/python2.7/site-packages/django/utils/itercompat.py", line 30, in is_iterator
    return isinstance(x, collections.Iterator)

  File "/home/rsaenz/virtualenvs/club/lib/python2.7/abc.py", line 144, in __instancecheck__
    return cls.__subclasscheck__(subtype)

  File "/home/rsaenz/virtualenvs/club/lib/python2.7/abc.py", line 184, in __subclasscheck__
    cls._abc_negative_cache.add(subclass)

  File "/home/rsaenz/virtualenvs/club/lib/python2.7/_weakrefset.py", line 86, in add
    self.data.add(ref(item, self._remove))

TypeError: weak object has gone away

thanks!

Martin Massera
  • 1,718
  • 1
  • 21
  • 47

1 Answers1

0

Based on the traceback this looks more like a Python weakrefset issue to me which collections.Iterator relies on through the abc (Abstract Base Class) module.

Simon Charette
  • 5,009
  • 1
  • 25
  • 33
  • so what do you think the solution would be? If it is a python problem then I would need to avoid these caches altogether? – Martin Massera Jun 09 '16 at 23:49
  • Did you find any solution to this problem? I'm also getting the same issue but have no clue where the problem is:https://stackoverflow.com/questions/58644906/weak-object-has-gone-away-what-does-it-mean?noredirect=1#comment103598008_58644906 – Alejandro Oct 31 '19 at 20:38