[PR #18264] [MERGED] Fixes: #18263 - Iterate through a freshly queried set of CableTerminations to find endpoints in update_connected_endpoints #15304

Closed
opened 2025-12-30 00:21:09 +01:00 by adam · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/netbox-community/netbox/pull/18264
Author: @bctiemann
Created: 12/21/2024
Status: Merged
Merged: 1/6/2025
Merged by: @jeremystretch

Base: developHead: 18263-fix-cable-api-patch-performance


📝 Commits (2)

  • b6265d8 Iterate through a freshly queried set of CableTerminations to find endpoints in update_connected_endpoints
  • d5468cc Add defensive break if q_filter has not been populated

📊 Changes

2 files changed (+6 additions, -1 deletions)

View changed files

📝 netbox/dcim/models/cables.py (+4 -0)
📝 netbox/dcim/signals.py (+2 -1)

📄 Description

Fixes: #18263

During an API PATCH update to a Cable where one or both endpoints is changed, the signal handler update_connected_endpoints iterates through self.terminations.all() which in the case of an API call has not necessarily been refreshed after the deletion of stale endpoints at b6265d8285/netbox/dcim/models/cables.py (L224-L240)

This leads to the local_cable_terminations being empty here (because the pks don't match): b6265d8285/netbox/dcim/models/cables.py (L597-L611) (Step 6 of Cable.from_origin), which leads to q_filter being empty and thus the completely unfiltered iteration at L611 potentially taking O(n) time depending on the number of terminations in the DB.

This change ensures that the iteration of CableTerminations associated with a Cable after an update is freshly queried from the DB following the deletion of stale endpoints rather than relying on the (potentially stale or cached) self.terminations reverse relation, and thus ensuring from_origin is calculated correctly.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/netbox-community/netbox/pull/18264 **Author:** [@bctiemann](https://github.com/bctiemann) **Created:** 12/21/2024 **Status:** ✅ Merged **Merged:** 1/6/2025 **Merged by:** [@jeremystretch](https://github.com/jeremystretch) **Base:** `develop` ← **Head:** `18263-fix-cable-api-patch-performance` --- ### 📝 Commits (2) - [`b6265d8`](https://github.com/netbox-community/netbox/commit/b6265d828526a97962e8feefa0ff57c534d2da27) Iterate through a freshly queried set of CableTerminations to find endpoints in update_connected_endpoints - [`d5468cc`](https://github.com/netbox-community/netbox/commit/d5468cc573dd5449b344bb327ed5896bd5302a37) Add defensive break if q_filter has not been populated ### 📊 Changes **2 files changed** (+6 additions, -1 deletions) <details> <summary>View changed files</summary> 📝 `netbox/dcim/models/cables.py` (+4 -0) 📝 `netbox/dcim/signals.py` (+2 -1) </details> ### 📄 Description ### Fixes: #18263 During an API PATCH update to a Cable where one or both endpoints is changed, the signal handler `update_connected_endpoints` iterates through `self.terminations.all()` which in the case of an API call has not necessarily been refreshed after the deletion of stale endpoints at https://github.com/netbox-community/netbox/blob/b6265d828526a97962e8feefa0ff57c534d2da27/netbox/dcim/models/cables.py#L224-L240 This leads to the `local_cable_terminations` being empty here (because the pks don't match): https://github.com/netbox-community/netbox/blob/b6265d828526a97962e8feefa0ff57c534d2da27/netbox/dcim/models/cables.py#L597-L611 (Step 6 of `Cable.from_origin`), which leads to `q_filter` being empty and thus the completely unfiltered iteration at L611 potentially taking O(n) time depending on the number of terminations in the DB. This change ensures that the iteration of CableTerminations associated with a Cable after an update is freshly queried from the DB following the deletion of stale endpoints rather than relying on the (potentially stale or cached) `self.terminations` reverse relation, and thus ensuring `from_origin` is calculated correctly. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
adam added the pull-request label 2025-12-30 00:21:09 +01:00
adam closed this issue 2025-12-30 00:21:10 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#15304