Unnable to save device configuration with a custom script and multiple threads #6749

Closed
opened 2025-12-29 19:44:57 +01:00 by adam · 1 comment
Owner

Originally created by @Mikeson on GitHub (Aug 1, 2022).

NetBox version

v3.2.7

Python version

3.10

Steps to Reproduce

  1. Create custom script ThreadTest.txt
  2. Run the script, and select "Commit changes"

Expected Behavior

Update all devices and save them

Observed Behavior

Traceback (most recent call last): File "/etc/netbox/scripts/ThreadTest.py", line 26, in assetThread device.save() File "/opt/netbox/netbox/dcim/models/devices.py", line 909, in save super().save(args, kwargs) File "/opt/netbox/venv/lib/python3.10/site-packages/django/db/models/base.py", line 806, in save self.save_base( File "/opt/netbox/venv/lib/python3.10/site-packages/django/db/models/base.py", line 872, in save_base post_save.send( File "/opt/netbox/venv/lib/python3.10/site-packages/django/dispatch/dispatcher.py", line 176, in send return [ File "/opt/netbox/venv/lib/python3.10/site-packages/django/dispatch/dispatcher.py", line 177, in (receiver, receiver(signal=self, sender=sender, *named)) File "/opt/netbox/netbox/extras/signals.py", line 67, in handle_changed_object objectchange.user = request.user AttributeError: 'NoneType' object has no attribute 'user'

Originally created by @Mikeson on GitHub (Aug 1, 2022). ### NetBox version v3.2.7 ### Python version 3.10 ### Steps to Reproduce 1. Create custom script [ThreadTest.txt](https://github.com/netbox-community/netbox/files/9232907/ThreadTest.txt) 2. Run the script, and select "Commit changes" ### Expected Behavior Update all devices and save them ### Observed Behavior Traceback (most recent call last): File "/etc/netbox/scripts/ThreadTest.py", line 26, in assetThread device.save() File "/opt/netbox/netbox/dcim/models/devices.py", line 909, in save super().save(args, kwargs) File "/opt/netbox/venv/lib/python3.10/site-packages/django/db/models/base.py", line 806, in save self.save_base( File "/opt/netbox/venv/lib/python3.10/site-packages/django/db/models/base.py", line 872, in save_base post_save.send( File "/opt/netbox/venv/lib/python3.10/site-packages/django/dispatch/dispatcher.py", line 176, in send return [ File "/opt/netbox/venv/lib/python3.10/site-packages/django/dispatch/dispatcher.py", line 177, in <listcomp> (receiver, receiver(signal=self, sender=sender, *named)) File "/opt/netbox/netbox/extras/signals.py", line 67, in handle_changed_object objectchange.user = request.user AttributeError: 'NoneType' object has no attribute 'user'
adam closed this issue 2025-12-29 19:44:57 +01:00
Author
Owner

@kkthxbye-code commented on GitHub (Aug 1, 2022):

Scripts are executed with changelog handling which uses a thread local context for the request context. As such your use case (threading) is not support in custom scripts.

If you want parallelism in your custom scripts you could try spawning new django-rq tasks instead.

If the example is representative of your use case, you would probably get better performance using bulk_update (this also omits signals, so no changelogs will be created). You can also use other hacks to avoid sending signals if you really want to save netbox objects in parallel.

Regardless I think this should be raised as a feature request instead, however I doubt this is something we would want to support explicitly.

@kkthxbye-code commented on GitHub (Aug 1, 2022): Scripts are executed with changelog handling which uses a thread local context for the request context. As such your use case (threading) is not support in custom scripts. If you want parallelism in your custom scripts you could try spawning new django-rq tasks instead. If the example is representative of your use case, you would probably get better performance using bulk_update (this also omits signals, so no changelogs will be created). You can also use [other hacks](https://stackoverflow.com/questions/1555060/how-to-save-a-model-without-sending-a-signal) to avoid sending signals if you really want to save netbox objects in parallel. Regardless I think this should be raised as a feature request instead, however I doubt this is something we would want to support explicitly.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#6749