From 84e415625968592106dd693094da4055df11846b Mon Sep 17 00:00:00 2001 From: kkthxbye-code Date: Fri, 22 Apr 2022 21:21:01 +0200 Subject: [PATCH] Add lock around script loading to prevent race condition --- netbox/extras/scripts.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/netbox/extras/scripts.py b/netbox/extras/scripts.py index 4eacddbeb..fc853a7a3 100644 --- a/netbox/extras/scripts.py +++ b/netbox/extras/scripts.py @@ -5,6 +5,7 @@ import os import pkgutil import sys import traceback +import threading from collections import OrderedDict import yaml @@ -42,6 +43,8 @@ __all__ = [ 'TextVar', ] +lock = threading.Lock() + # # Script variables @@ -491,11 +494,14 @@ def get_scripts(use_names=False): # Iterate through all modules within the scripts path. These are the user-created files in which reports are # defined. for importer, module_name, _ in pkgutil.iter_modules([settings.SCRIPTS_ROOT]): - # Remove cached module to ensure consistency with filesystem - if module_name in sys.modules: - del sys.modules[module_name] + # Use a lock as removing and loading modules is not thread safe + with lock: + # Remove cached module to ensure consistency with filesystem + if module_name in sys.modules: + del sys.modules[module_name] + + module = importer.find_module(module_name).load_module(module_name) - module = importer.find_module(module_name).load_module(module_name) if use_names and hasattr(module, 'name'): module_name = module.name module_scripts = OrderedDict()