Skip to content

Conversation

@pheus
Copy link
Contributor

@pheus pheus commented Oct 28, 2025

Fixes: #20697

Summary
Prevent double counting in CounterCacheField by ensuring signal receivers are connected once per sender.

Implementation

  • Update utilities.counters.connect_counters() to use per‑sender dispatch_uid for post_save, pre_delete, and post_delete.
  • Guard against reconnecting by tracking models already processed during registration.
  • Leave a single receiver to update all registered counters for the same sender (e.g., device_count and member_count).

Impact

  • Fixes +2/−2 updates when multiple counters share the same child model (e.g., Device).
  • No database changes; backward‑compatible internal wiring change.

Updates `connect_counters` to prevent duplicate signal handlers by
using consistent `dispatch_uid` values per sender. Adds a check to
avoid reconnecting models already processed during registration.

Fixes netbox-community#20697
@pheus
Copy link
Contributor Author

pheus commented Oct 28, 2025

Note: This bug only manifests when multiple counters are registered for the same model/sender. That isn’t the case in core yet, so I didn’t add a failing test here without introducing test‑only fields. Once the PR for #19523 is ready, it will include a second counter for Device, which gives us a clean repro to test against. I’m happy to add/enable the test in that PR (or as a follow‑up). I hope that’s acceptable.

@jeremystretch jeremystretch self-requested a review October 28, 2025 17:01
@pheus
Copy link
Contributor Author

pheus commented Oct 28, 2025

I can push my current WIP for device_count and module_count as a draft PR to make it easier to review this change. Let me know if that would be helpful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Per‑sender duplicate signal registration causes CounterCacheField double‑counting

1 participant