Event Rule on MACAddress - Custom Script fail with error #11567

Closed
opened 2025-12-29 21:46:51 +01:00 by adam · 4 comments
Owner

Originally created by @stefanoparis-mgh on GitHub (Sep 4, 2025).

Originally assigned to: @jnovinger on GitHub.

Deployment Type

Self-hosted

NetBox Version

v4.4.0

Python Version

3.11

Steps to Reproduce

  1. Define simple custom script that log a row (script attached)
  2. Create Event Rule on MACAddress Create/Update that execute previously created script
  3. Edit MACAddress

Script can be executed manually without error. In version 4.3.7 script worked correctly

Test.py

Expected Behavior

Script get executed with a simple log row

Observed Behavior

Error raised:
AttributeError("'NoneType' object has no attribute 'id'")

Originally created by @stefanoparis-mgh on GitHub (Sep 4, 2025). Originally assigned to: @jnovinger on GitHub. ### Deployment Type Self-hosted ### NetBox Version v4.4.0 ### Python Version 3.11 ### Steps to Reproduce 1. Define simple custom script that log a row (script attached) 2. Create Event Rule on MACAddress Create/Update that execute previously created script 3. Edit MACAddress Script can be executed manually without error. In version 4.3.7 script worked correctly [Test.py](https://github.com/user-attachments/files/22132515/Test.py) ### Expected Behavior Script get executed with a simple log row ### Observed Behavior Error raised: AttributeError("'NoneType' object has no attribute 'id'")
adam added the type: bugstatus: acceptedseverity: low labels 2025-12-29 21:46:51 +01:00
adam closed this issue 2025-12-29 21:46:51 +01:00
Author
Owner

@stefanoparis-mgh commented on GitHub (Sep 4, 2025):

Same with Event Rule on Journal Entry or Device created/updated

@stefanoparis-mgh commented on GitHub (Sep 4, 2025): Same with Event Rule on Journal Entry or Device created/updated
Author
Owner

@miked314 commented on GitHub (Sep 10, 2025):

I'm getting same error. I took similar steps as @stefanoparis-mgh to confirm it wasn't my script.
here is output from netbox-rq

Sep 09 22:33:10  python3[1290826]: 17:33:10 default: handle(data={'id': 3280, 'url': '/api/dcim/devices/3280/', 'display_url': '/dcim/device..., job=<Job: test sync>) (2f1be4ee-3454-405c-be8d-44a5d7cb8add)
Sep 09 22:33:10  python3[1292870]: 17:33:10 Successfully completed handle(data={'id': 3280, 'url': '/api/dcim/devices/3280/', 'display_url': '/dcim/device..., job=<Job: test sync>) job in 0:00:00.031599s on worker aae833a4574b4c96809eeb964b164be7
Sep 09 22:33:10  python3[1292870]: 17:33:10 default: Job OK (2f1be4ee-3454-405c-be8d-44a5d7cb8add)
Sep 09 22:33:10  python3[1292870]: 17:33:10 Result is kept for 500 seconds

Error raised:

AttributeError("'NoneType' object has no attribute 'id'")
@miked314 commented on GitHub (Sep 10, 2025): I'm getting same error. I took similar steps as @stefanoparis-mgh to confirm it wasn't my script. here is output from netbox-rq ``` Sep 09 22:33:10 python3[1290826]: 17:33:10 default: handle(data={'id': 3280, 'url': '/api/dcim/devices/3280/', 'display_url': '/dcim/device..., job=<Job: test sync>) (2f1be4ee-3454-405c-be8d-44a5d7cb8add) Sep 09 22:33:10 python3[1292870]: 17:33:10 Successfully completed handle(data={'id': 3280, 'url': '/api/dcim/devices/3280/', 'display_url': '/dcim/device..., job=<Job: test sync>) job in 0:00:00.031599s on worker aae833a4574b4c96809eeb964b164be7 Sep 09 22:33:10 python3[1292870]: 17:33:10 default: Job OK (2f1be4ee-3454-405c-be8d-44a5d7cb8add) Sep 09 22:33:10 python3[1292870]: 17:33:10 Result is kept for 500 seconds ``` Error raised: ``` AttributeError("'NoneType' object has no attribute 'id'") ```
Author
Owner

@rfdrake commented on GitHub (Sep 10, 2025):

I was able to duplicate this:

In the Job Log I saw this:
Image

In the logs for netbox-worker it shows that the job completed OK:.

netbox-worker-1  | 20:18:59 default: handle(data={'id': 155, 'url': '/api/dcim/devices/155/', 'display_url': '/dcim/devices/..., job=<Job: Test Script>) (b856a589-44af-4a70-970a-7f89e3a42931)
netbox-worker-1  | 20:19:00 Successfully completed handle(data={'id': 155, 'url': '/api/dcim/devices/155/', 'display_url': '/dcim/devices/..., job=<Job: Test Script>) job in 0:00:00.181012s on worker a6b02f245c97439baba44b8e1a26f623
netbox-worker-1  | 20:19:00 default: Job OK (b856a589-44af-4a70-970a-7f89e3a42931)

Simulating it in nbshell:

from extras.models import EventRule
from extras.jobs import ScriptJob
from users.models import User

er = EventRule.objects.get(name="test")
script = er.action_object.python_class()
test_job = ScriptJob.enqueue(instance=er.action_object, name=script.name, user=User.objects.get(username="admin"), data=None)

When I run this I can see the event processed with netbox-worker. I see the error in the Jobs console.

So I dug into ScriptJobs.run(). From what I can see, this is probably the culprit:

        self.logger.debug(f"Request ID: {request.id}")

I think in this particular case the request is None. I think this happens in two scenarios, one being events fired from event rules, and the other being scripts run on the CLI with runscript.

In runscript's case, it should still be working because it fakes the request, which gives it an id.

        job = ScriptJob.enqueue(
            instance=script_obj,
            user=user,
            immediate=True,
            data=form.cleaned_data,
            request=NetBoxFakeRequest({
                'META': {},
                'POST': data,
                'GET': {},
                'FILES': {},
                'user': user,
                'path': '',
                'id': uuid.uuid4()
            }),
            commit=commit,

So the simple fix would be edit jobs.py and change that debug line to make request.id optional (maybe move it two lines up under "if request:"

@rfdrake commented on GitHub (Sep 10, 2025): I was able to duplicate this: In the Job Log I saw this: <img width="889" height="210" alt="Image" src="https://github.com/user-attachments/assets/05f55294-4bfd-4d3d-a76f-2ae15e6795e7" /> In the logs for netbox-worker it shows that the job completed OK:. netbox-worker-1 | 20:18:59 default: handle(data={'id': 155, 'url': '/api/dcim/devices/155/', 'display_url': '/dcim/devices/..., job=<Job: Test Script>) (b856a589-44af-4a70-970a-7f89e3a42931) netbox-worker-1 | 20:19:00 Successfully completed handle(data={'id': 155, 'url': '/api/dcim/devices/155/', 'display_url': '/dcim/devices/..., job=<Job: Test Script>) job in 0:00:00.181012s on worker a6b02f245c97439baba44b8e1a26f623 netbox-worker-1 | 20:19:00 default: Job OK (b856a589-44af-4a70-970a-7f89e3a42931) Simulating it in nbshell: ``` from extras.models import EventRule from extras.jobs import ScriptJob from users.models import User er = EventRule.objects.get(name="test") script = er.action_object.python_class() test_job = ScriptJob.enqueue(instance=er.action_object, name=script.name, user=User.objects.get(username="admin"), data=None) ``` When I run this I can see the event processed with netbox-worker. I see the error in the Jobs console. So I dug into ScriptJobs.run(). From what I can see, this is probably the culprit: ``` self.logger.debug(f"Request ID: {request.id}") ``` I think in this particular case the request is None. I think this happens in two scenarios, one being events fired from event rules, and the other being scripts run on the CLI with runscript. In runscript's case, it should still be working because it fakes the request, which gives it an id. ``` job = ScriptJob.enqueue( instance=script_obj, user=user, immediate=True, data=form.cleaned_data, request=NetBoxFakeRequest({ 'META': {}, 'POST': data, 'GET': {}, 'FILES': {}, 'user': user, 'path': '', 'id': uuid.uuid4() }), commit=commit, ``` So the simple fix would be edit jobs.py and change that debug line to make request.id optional (maybe move it two lines up under "if request:"
Author
Owner

@miked314 commented on GitHub (Sep 10, 2025):

@rfdrake - Thank you!

@miked314 commented on GitHub (Sep 10, 2025): @rfdrake - Thank you!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#11567