Improve hot-reloading of custom scripts #5648

Closed
opened 2025-12-29 19:30:39 +01:00 by adam · 2 comments
Owner

Originally created by @kkthxbye-code on GitHub (Nov 11, 2021).

Originally assigned to: @kkthxbye-code on GitHub.

NetBox version

V3.0.9

Feature type

Change to existing functionality

Proposed functionality

Allow renaming of classes in custom scripts. Currently if you rename a class or remove a class from a file, netbox will throw OSError : could not find class definition and the script functionality will be broken until the application server is restarted.

The reason for this is that python uses sys.modules to cache all imported modules. When you change a class in the script and reload it in get_scripts, which is called on both the list view and when executing scripts, the module includes both the old name for the class and the new name (not sure why), causing the import to fail on the old name.

The solution is pretty simple, deleting the module from sys.modules. A simple implementation can be seen here:

ae6ed97a80

I wasn't able to notice any performance regressions, the extra load should be minimal even with a lot of scripts. The advantage is that the loaded script is actually correct.

Use case

Scripts already allow changing code without restart, with the requested change you will also be able to rename or delete script classes.

Database changes

None

External dependencies

None

Originally created by @kkthxbye-code on GitHub (Nov 11, 2021). Originally assigned to: @kkthxbye-code on GitHub. ### NetBox version V3.0.9 ### Feature type Change to existing functionality ### Proposed functionality Allow renaming of classes in custom scripts. Currently if you rename a class or remove a class from a file, netbox will throw `OSError : could not find class definition` and the script functionality will be broken until the application server is restarted. The reason for this is that python uses sys.modules to cache all imported modules. When you change a class in the script and reload it in get_scripts, which is called on both the list view and when executing scripts, the module includes both the old name for the class and the new name (not sure why), causing the import to fail on the old name. The solution is pretty simple, deleting the module from sys.modules. A simple implementation can be seen here: https://github.com/kkthxbye-code/netbox/commit/ae6ed97a80f55e87cd75dc0e942069758a1cd38d I wasn't able to notice any performance regressions, the extra load should be minimal even with a lot of scripts. The advantage is that the loaded script is actually correct. ### Use case Scripts already allow changing code without restart, with the requested change you will also be able to rename or delete script classes. ### Database changes None ### External dependencies None
adam added the status: acceptedtype: feature labels 2025-12-29 19:30:39 +01:00
adam closed this issue 2025-12-29 19:30:39 +01:00
Author
Owner

@kkthxbye-code commented on GitHub (Nov 11, 2021):

Note: I wasn't sure if this constituted a bug report or a feature request. Scripts are made to be hot reloaded as far as I can tell, so it would make sense to fix this as a bug.

Related old tickets: #3579 - #1590

@kkthxbye-code commented on GitHub (Nov 11, 2021): Note: I wasn't sure if this constituted a bug report or a feature request. Scripts are made to be hot reloaded as far as I can tell, so it would make sense to fix this as a bug. Related old tickets: #3579 - #1590
Author
Owner

@jeremystretch commented on GitHub (Nov 11, 2021):

Nice, makes sense to me. @kkthxbye-code assigned this to you for a PR to merge your changes. Thanks!

@jeremystretch commented on GitHub (Nov 11, 2021): Nice, makes sense to me. @kkthxbye-code assigned this to you for a PR to merge your changes. Thanks!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#5648