Skip to content

Commit 640a0ab

Browse files
committed
keep global lock when checking cache
1 parent d2f075f commit 640a0ab

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

netbox_custom_objects/models.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -447,11 +447,12 @@ def get_model(
447447
"""
448448

449449
# Double-check pattern: check cache again after acquiring lock
450-
if self.is_model_cached(self.id) and not no_cache:
451-
model = self.get_cached_model(self.id)
452-
return model
450+
with self._global_lock:
451+
if self.is_model_cached(self.id) and not no_cache:
452+
model = self.get_cached_model(self.id)
453+
return model
453454

454-
# Generate the model inside the lock to prevent race conditions
455+
# Generate the model outside the lock to avoid holding it during expensive operations
455456
model_name = self.get_table_model_name(self.pk)
456457

457458
# TODO: Add other fields with "index" specified
@@ -524,8 +525,9 @@ def wrapped_post_through_setup(self, cls):
524525

525526
self._after_model_generation(attrs, model)
526527

527-
# Cache the generated model
528-
self._model_cache[self.id] = model
528+
# Cache the generated model (protected by lock for thread safety)
529+
with self._global_lock:
530+
self._model_cache[self.id] = model
529531

530532
# Do the clear cache now that we have it in the cache so there
531533
# is no recursion.

0 commit comments

Comments
 (0)