[bug] Repeated device_group_changed signal on subsequent saves of the same device instance
I maintainer di solito rispondono entro 1 giorno
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 2/5
- Tempo stimato
- 1-3 ore
- Idoneità per principianti
- 72/100
Direzione di ricerca
Inizia in openwisp_controller/config/base/device.py da _check_group_id_changed(), che emette il segnale di cambio gruppo ma non aggiorna mai _initial_group_id, e confrontalo con _check_management_ip_changed(), che invece aggiorna la sua baseline. Aggiungi un test di regressione che mantenga una singola istanza di dispositivo attraverso il passaggio da A a B, un salvataggio senza modifiche e poi il passaggio da B a C, e verifichi il numero di segnali e old_group_id. Il lavoro è completato quando solo le transizioni reali emettono un segnale una volta e ciascuna riporta il gruppo immediatamente precedente.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Describe the bug
The device_group_changed signal is emitted repeatedly when saving the same device model instance, even when its group has not changed since the previous save.
The issue originates in AbstractDevice._check_group_id_changed(). The method compares the current group_id against _initial_group_id and emits the signal when they differ, but does not update _initial_group_id after detecting the change.
Consequently, the original group ID remains the comparison baseline for subsequent saves of the same Python model instance. This results in duplicate signal emissions and an incorrect old_group_id being passed to signal receivers during subsequent genuine group changes.
This is particularly relevant because the signal is used by the configuration application to trigger downstream operations, including device-template reconciliation. Repeated emissions for a single group transition may therefore schedule unnecessary downstream work.
Steps to Reproduce
- Retrieve an existing device belonging to Group A from the database.
- Keep the retrieved device instance in memory throughout the following operations. Do not refresh or reload it.
- Change its group from Group A to Group B and save the instance.
- Save the same instance again without making any additional changes to its group.
- Change its group from Group B to Group C and save the same instance again.
- Observe the number of
device_group_changedsignal emissions and inspect theold_group_idandgroup_idarguments received by the signal handlers.
Actual Behavior
The signal is emitted on every subsequent save because _initial_group_id continues to hold the original Group A ID.
The reproduced results were:
| Save operation | Signals emitted by this save | Cumulative signal count | Expected cumulative count |
|---|---|---|---|
| Group A → Group B | 1 | 1 | 1 |
| Save unchanged Group B | 1 | 2 | 1 |
| Group B → Group C | 1 | 3 | 2 |
Additionally, during the final Group B → Group C transition, the signal incorrectly reports Group A as old_group_id, instead of the immediately preceding group, Group B.
Expected Behavior
The device_group_changed signal should be emitted only when the device's group actually changes.
For the reproduction sequence above, the expected behavior is:
| Save operation | Expected signal emissions | Expected old_group_id |
Expected group_id |
|---|---|---|---|
| Group A → Group B | 1 | Group A | Group B |
| Save unchanged Group B | 0 | — | — |
| Group B → Group C | 1 | Group B | Group C |
Specifically:
- Saving an unchanged device instance should not emit
device_group_changed. - Each genuine group transition should emit the signal exactly once.
- The signal should report the immediately previous group as
old_group_id. - Existing behavior for device creation and legitimate group changes should remain unaffected.
Technical Investigation
The behavior appears to originate in:
openwisp_controller/config/base/device.py
The relevant methods are:
AbstractDevice.__init__()_set_initial_values_for_changed_checked_fields()_check_changed_fields()_check_group_id_changed()_check_management_ip_changed()
During initialization, _initial_group_id is populated through _set_initial_values_for_changed_checked_fields().
On subsequent saves, _check_changed_fields() invokes _check_group_id_changed(). When the current group_id differs from _initial_group_id, the method calls _send_device_group_changed_signal().
However, the tracked baseline is not advanced after the signal is emitted.
A comparable implementation exists in _check_management_ip_changed(), which updates _initial_management_ip after detecting a change. The group-change implementation does not currently perform the equivalent baseline update.
Deferred-field handling was also examined and does not appear to explain the behavior for an ordinarily loaded device with a non-deferred group baseline.
Reproduction and Test Results
The issue was reproduced using executable Django tests against commit 87585e4, with:
- Python 3.14.4
- Django 6.1.3
TransactionTestCase- Actual signal receivers
- Committed database saves
The regression test retained the same Python model instance throughout the sequence and verified the persisted group after each operation without refreshing or replacing the instance.
Additional verification:
- 62 existing targeted tests passed, covering device, device-group, and related admin tests.
- 47 existing sample-model tests passed with
SAMPLE_APP=1. - The new group-change regression failed in both configurations, reproducing the duplicate signal emissions and stale
old_group_id. - A management-IP control test passed in both configurations, confirming the expected cumulative signal counts of
1, 1, 2for the equivalent change/save sequence.
The existing group-signal tests cover individual group changes and device creation, but do not appear to cover repeated saves of the same model instance after a group transition.
Suggested Resolution
The group-change tracking logic should maintain an accurate baseline after a genuine group change, so subsequent saves compare against the most recently recorded group rather than the original group.
Regression coverage should verify that:
- A genuine group change emits
device_group_changedexactly once. - Saving the same instance again without changing its group does not emit another signal.
- A second genuine group change on the same instance emits exactly one additional signal.
- The
old_group_idandgroup_idarguments correctly represent each individual transition. - Existing device creation and group-change behavior remains unaffected.
The implementation approach can be determined based on the project's existing model lifecycle and change-tracking conventions.
Additional Information
This issue has been reproduced locally and investigated against the current implementation. No production source changes have been made as part of the investigation.
I would be happy to work on a focused fix and the associated regression tests if the maintainers agree that this is an appropriate issue to address.
Thank you for your time and for maintaining OpenWISP.
- Lingua principale
- Python
- Stelle
- 800
- Fork
- 354
- Merge medio
- 1g 8h
- PR unite (30g)
- 15
Preparare l'ambiente
- Include un Dockerfile o un file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di openwisp/openwisp-controller
-
[bug] `Device.manage_devices_group_templates` aborts early when any device lacks a configForse già presa @blade0g l’ha presa 4 giorni fa. Apertabug
Difficoltà 2/5 1-3 ore Idoneità per principianti 85/100
openwisp/openwisp-controller#1572 · 2 commenti ·
I maintainer di solito rispondono entro 1 giorno
-
[bug] Test project RedisChannelLayer times out after 5s with redis-py 8Forse di nuovo libera Una pull request per questa issue è stata chiusa senza essere unita. Aperta
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 85/100
openwisp/openwisp-controller#1569 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
[bug] DeviceListFilterBackend drops additional query filters when config__templates is providedForse già presa @rohanesor l’ha presa 46 giorni fa. Apertabug
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
openwisp/openwisp-controller#1544 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
[fix] Recent Commands row uses "created" timestamp for the "Modified" fieldForse già presa @xovishnukosuri l’ha presa 263 giorni fa. Apertabug
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 78/100
openwisp/openwisp-controller#1263 · 4 commenti ·
I maintainer di solito rispondono entro 1 giorno
-
[bug] Recent Commands clips long inputs and formats them differently before and after reloadForse già presa @vanshwadhwa24 l’ha presa oggi. Apertabug
openwisp/openwisp-controller#1577 · 3 commenti · 1 assegnatario ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di openwisp/openwisp-controller
Issue simili
-
fidelity
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
I maintainer di solito rispondono entro 1 giorno
-
backlog
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
sagemathinc/sagebrush#3434 ·
I maintainer di solito rispondono entro 1 giorno
-
marketing
Difficoltà 2/5 1-3 ore Idoneità per principianti 85/100
I maintainer di solito rispondono entro 1 giorno
-
documentation good first issue help wanted
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
wuisabel-gif/MemWhale#576 ·
I maintainer di solito rispondono entro 1 giorno
-
Status: Triage Suggestion
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
GTNewHorizons/GT-New-Horizons-Modpack#27957 ·
I maintainer di solito rispondono entro 1 giorno