Exception on object change - AnonymousUser object - race condition? #2960

Closed
opened 2025-12-29 18:24:03 +01:00 by adam · 6 comments
Owner

Originally created by @candlerb on GitHub (Oct 18, 2019).

Environment

  • Python version: 3.5.2
  • NetBox version: 2.6.6

Steps to Reproduce

python3 manage.py nbshell
>>> ip = IPAddress(address = '192.168.43.56/24', family=4, status=1, description='test')
>>> ip.save()
>>> ip.id
142

In the GUI, I am logged in as an admin user. Go to /ipam/ip-addresses/142/

Click "Delete this IP" and "Confirm"

Expected Behavior

Normal deletion

Observed Behavior

Sometimes I get an exception (but only small proportion of cases):

Internal Server Error: /ipam/ip-addresses/142/delete/

ValueError at /ipam/ip-addresses/142/delete/
Cannot assign "<django.contrib.auth.models.AnonymousUser object at 0x7fdd399415c0>": "ObjectChange.user" must be a "User" instance.

Request Method: POST
Request URL: http://netbox.example.net/ipam/ip-addresses/142/delete/
Django Version: 2.2.6
Python Executable: /usr/bin/python3
Python Version: 3.5.2
Python Path: ['/opt/netbox/netbox', '/usr/local/bin', '/usr/lib/python35.zip', '/usr/lib/python3.5', '/usr/lib/python3.5/plat-x86_64-linux-gnu', '/usr/lib/python3.5/lib-dynload', '/usr/local/lib/python3.5/dist-packages', '/usr/lib/python3/dist-packages']
Server time: Fri, 18 Oct 2019 09:33:39 +0000
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.humanize',
 'cacheops',
 'corsheaders',
 'debug_toolbar',
 'django_filters',
 'django_tables2',
 'django_prometheus',
 'mptt',
 'rest_framework',
 'taggit',
 'taggit_serializer',
 'timezone_field',
 'circuits',
 'dcim',
 'ipam',
 'extras',
 'secrets',
 'tenancy',
 'users',
 'utilities',
 'virtualization',
 'drf_yasg',
 'django_rq']
Installed Middleware:
('debug_toolbar.middleware.DebugToolbarMiddleware',
 'django_prometheus.middleware.PrometheusBeforeMiddleware',
 'corsheaders.middleware.CorsMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'django.middleware.security.SecurityMiddleware',
 'utilities.middleware.ExceptionHandlingMiddleware',
 'utilities.middleware.LoginRequiredMiddleware',
 'utilities.middleware.APIVersionMiddleware',
 'extras.middleware.ObjectChangeMiddleware',
 'django_prometheus.middleware.PrometheusAfterMiddleware')


Traceback:

File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/exception.py" in inner
  34.             response = get_response(request)

File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py" in _get_response
  115.                 response = self.process_exception_by_middleware(e, request)

File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py" in _get_response
  113.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/usr/local/lib/python3.5/dist-packages/django/views/generic/base.py" in view
  71.             return self.dispatch(request, *args, **kwargs)

File "/usr/local/lib/python3.5/dist-packages/django/contrib/auth/mixins.py" in dispatch
  85.         return super().dispatch(request, *args, **kwargs)

File "/usr/local/lib/python3.5/dist-packages/django/views/generic/base.py" in dispatch
  97.         return handler(request, *args, **kwargs)

File "/opt/netbox/netbox/utilities/views.py" in post
  290.                 obj.delete()

File "/usr/local/lib/python3.5/dist-packages/django/db/models/base.py" in delete
  919.         return collector.delete()

File "/usr/local/lib/python3.5/dist-packages/django/db/models/deletion.py" in delete
  318.                             sender=model, instance=obj, using=self.using

File "/usr/local/lib/python3.5/dist-packages/django/dispatch/dispatcher.py" in send
  175.             for receiver in self._live_receivers(sender)

File "/usr/local/lib/python3.5/dist-packages/django/dispatch/dispatcher.py" in <listcomp>
  175.             for receiver in self._live_receivers(sender)

File "/usr/local/lib/python3.5/dist-packages/django/utils/functional.py" in _curried
  14.         return _curried_func(*args, *moreargs, **{**kwargs, **morekwargs})

File "/opt/netbox/netbox/extras/middleware.py" in _handle_deleted_object
  40.         objectchange.user = request.user

File "/usr/local/lib/python3.5/dist-packages/django/db/models/fields/related_descriptors.py" in __set__
  211.                     self.field.remote_field.model._meta.object_name,

Exception Type: ValueError at /ipam/ip-addresses/142/delete/
Exception Value: Cannot assign "<django.contrib.auth.models.AnonymousUser object at 0x7fdd399415c0>": "ObjectChange.user" must be a "User" instance.
Request information:
USER: admin

GET: No GET data

POST:
confirm = 'True'
return_url = ''
_confirm = ''
Originally created by @candlerb on GitHub (Oct 18, 2019). ### Environment * Python version: 3.5.2 * NetBox version: 2.6.6 ### Steps to Reproduce ``` python3 manage.py nbshell >>> ip = IPAddress(address = '192.168.43.56/24', family=4, status=1, description='test') >>> ip.save() >>> ip.id 142 ``` In the GUI, I am logged in as an admin user. Go to `/ipam/ip-addresses/142/` Click "Delete this IP" and "Confirm" ### Expected Behavior Normal deletion ### Observed Behavior Sometimes I get an exception (but only small proportion of cases): ``` Internal Server Error: /ipam/ip-addresses/142/delete/ ValueError at /ipam/ip-addresses/142/delete/ Cannot assign "<django.contrib.auth.models.AnonymousUser object at 0x7fdd399415c0>": "ObjectChange.user" must be a "User" instance. Request Method: POST Request URL: http://netbox.example.net/ipam/ip-addresses/142/delete/ Django Version: 2.2.6 Python Executable: /usr/bin/python3 Python Version: 3.5.2 Python Path: ['/opt/netbox/netbox', '/usr/local/bin', '/usr/lib/python35.zip', '/usr/lib/python3.5', '/usr/lib/python3.5/plat-x86_64-linux-gnu', '/usr/lib/python3.5/lib-dynload', '/usr/local/lib/python3.5/dist-packages', '/usr/lib/python3/dist-packages'] Server time: Fri, 18 Oct 2019 09:33:39 +0000 Installed Applications: ['django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'django.contrib.humanize', 'cacheops', 'corsheaders', 'debug_toolbar', 'django_filters', 'django_tables2', 'django_prometheus', 'mptt', 'rest_framework', 'taggit', 'taggit_serializer', 'timezone_field', 'circuits', 'dcim', 'ipam', 'extras', 'secrets', 'tenancy', 'users', 'utilities', 'virtualization', 'drf_yasg', 'django_rq'] Installed Middleware: ('debug_toolbar.middleware.DebugToolbarMiddleware', 'django_prometheus.middleware.PrometheusBeforeMiddleware', 'corsheaders.middleware.CorsMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', 'django.middleware.security.SecurityMiddleware', 'utilities.middleware.ExceptionHandlingMiddleware', 'utilities.middleware.LoginRequiredMiddleware', 'utilities.middleware.APIVersionMiddleware', 'extras.middleware.ObjectChangeMiddleware', 'django_prometheus.middleware.PrometheusAfterMiddleware') Traceback: File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/exception.py" in inner 34. response = get_response(request) File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py" in _get_response 115. response = self.process_exception_by_middleware(e, request) File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py" in _get_response 113. response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/usr/local/lib/python3.5/dist-packages/django/views/generic/base.py" in view 71. return self.dispatch(request, *args, **kwargs) File "/usr/local/lib/python3.5/dist-packages/django/contrib/auth/mixins.py" in dispatch 85. return super().dispatch(request, *args, **kwargs) File "/usr/local/lib/python3.5/dist-packages/django/views/generic/base.py" in dispatch 97. return handler(request, *args, **kwargs) File "/opt/netbox/netbox/utilities/views.py" in post 290. obj.delete() File "/usr/local/lib/python3.5/dist-packages/django/db/models/base.py" in delete 919. return collector.delete() File "/usr/local/lib/python3.5/dist-packages/django/db/models/deletion.py" in delete 318. sender=model, instance=obj, using=self.using File "/usr/local/lib/python3.5/dist-packages/django/dispatch/dispatcher.py" in send 175. for receiver in self._live_receivers(sender) File "/usr/local/lib/python3.5/dist-packages/django/dispatch/dispatcher.py" in <listcomp> 175. for receiver in self._live_receivers(sender) File "/usr/local/lib/python3.5/dist-packages/django/utils/functional.py" in _curried 14. return _curried_func(*args, *moreargs, **{**kwargs, **morekwargs}) File "/opt/netbox/netbox/extras/middleware.py" in _handle_deleted_object 40. objectchange.user = request.user File "/usr/local/lib/python3.5/dist-packages/django/db/models/fields/related_descriptors.py" in __set__ 211. self.field.remote_field.model._meta.object_name, Exception Type: ValueError at /ipam/ip-addresses/142/delete/ Exception Value: Cannot assign "<django.contrib.auth.models.AnonymousUser object at 0x7fdd399415c0>": "ObjectChange.user" must be a "User" instance. Request information: USER: admin GET: No GET data POST: confirm = 'True' return_url = '' _confirm = '' ```
adam added the type: bug label 2025-12-29 18:24:03 +01:00
adam closed this issue 2025-12-29 18:24:03 +01:00
Author
Owner

@jeremystretch commented on GitHub (Oct 21, 2019):

I'm pretty sure this is a different manifestation of the same bug in #3309. I'm going to leave this one open until we know for certain though.

After more digging into #3309, I believe these are separate issues.

@jeremystretch commented on GitHub (Oct 21, 2019): ~I'm _pretty_ sure this is a different manifestation of the same bug in #3309. I'm going to leave this one open until we know for certain though.~ After more digging into #3309, I believe these are separate issues.
Author
Owner

@jeremystretch commented on GitHub (Oct 22, 2019):

@candlerb If you have time could you try testing against the 3309-changelog-middleware branch? I ended up rewriting a good portion of the change logging logic and I have a hunch it might address this issue as well.

@jeremystretch commented on GitHub (Oct 22, 2019): @candlerb If you have time could you try testing against the `3309-changelog-middleware` branch? I ended up rewriting a good portion of the change logging logic and I have a hunch it might address this issue as well.
Author
Owner

@candlerb commented on GitHub (Oct 23, 2019):

I've done some basic testing:

  • webhooks still work as before
  • so far I haven't been able to reproduce the problem originally shown above. However, it wasn't easy to reproduce even beforehand :-(

The new logic does look cleaner though (i.e. deepcopy of object prior to deletion), so more likely to be correct.

I wonder if this were done in post_init rather than pre_delete that it might also solve #3451?

@candlerb commented on GitHub (Oct 23, 2019): I've done some basic testing: * webhooks still work as before * so far I haven't been able to reproduce the problem originally shown above. However, it wasn't easy to reproduce even beforehand :-( The new logic does look cleaner though (i.e. deepcopy of object prior to deletion), so more likely to be correct. I wonder if this were done in `post_init` rather than `pre_delete` that it might also solve #3451?
Author
Owner

@jeremystretch commented on GitHub (Oct 25, 2019):

Marking as blocked pending further testing against the changes introduced by #3632.

@jeremystretch commented on GitHub (Oct 25, 2019): Marking as blocked pending further testing against the changes introduced by #3632.
Author
Owner

@jeremystretch commented on GitHub (Nov 6, 2019):

This was probably fixed by #3632. Has anyone been able to reproduce this on v2.6.7 or later?

@jeremystretch commented on GitHub (Nov 6, 2019): This was probably fixed by #3632. Has anyone been able to reproduce this on v2.6.7 or later?
Author
Owner

@candlerb commented on GitHub (Nov 6, 2019):

Happy to close, and reopen if it occurs again.

@candlerb commented on GitHub (Nov 6, 2019): Happy to close, and reopen if it occurs again.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#2960