[bug] Repeated device_group_changed signal on subsequent saves of the same device instance
Los mantenedores suelen responder en 1 día
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 2/5
- Tiempo estimado
- 1-3 horas
- Aptitud para principiantes
- 72/100
Línea de trabajo
Empieza en openwisp_controller/config/base/device.py en _check_group_id_changed(), que emite la señal de cambio de grupo pero nunca actualiza _initial_group_id, y compáralo con _check_management_ip_changed(), que sí actualiza su línea base. Añade una prueba de regresión que mantenga una única instancia de dispositivo a través de A a B, un guardado sin cambios y luego B a C, y verifica el número de señales y old_group_id. Está terminado cuando solo las transiciones reales emiten una vez y cada una informa del grupo inmediatamente anterior.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
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.
- Lenguaje dominante
- Python
- Estrellas
- 800
- Forks
- 354
- Merge medio
- 1 d 8 h
- PR fusionados (30 d)
- 15
Preparar el entorno
- Incluye un Dockerfile o un archivo de Docker Compose
- Tiene una plantilla de pull request
- Leer la guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de openwisp/openwisp-controller
-
[bug] `Device.manage_devices_group_templates` aborts early when any device lacks a configPosiblemente ocupada @blade0g la tomó hace 4 días. Abiertobug
Dificultad 2/5 1-3 horas Aptitud para principiantes 85/100
openwisp/openwisp-controller#1572 · 2 comentarios ·
Los mantenedores suelen responder en 1 día
-
[bug] Test project RedisChannelLayer times out after 5s with redis-py 8Quizá libre de nuevo Un pull request para esta issue se cerró sin fusionarse. Abierto
Dificultad 1/5 Menos de una hora Aptitud para principiantes 85/100
openwisp/openwisp-controller#1569 · 1 comentario ·
Los mantenedores suelen responder en 1 día
-
[bug] DeviceListFilterBackend drops additional query filters when config__templates is providedPosiblemente ocupada @rohanesor la tomó hace 46 días. Abiertobug
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
openwisp/openwisp-controller#1544 · 1 comentario ·
Los mantenedores suelen responder en 1 día
-
[fix] Recent Commands row uses "created" timestamp for the "Modified" fieldPosiblemente ocupada @xovishnukosuri la tomó hace 263 días. Abiertobug
Dificultad 1/5 Menos de una hora Aptitud para principiantes 78/100
openwisp/openwisp-controller#1263 · 4 comentarios ·
Los mantenedores suelen responder en 1 día
-
[bug] Recent Commands clips long inputs and formats them differently before and after reloadPosiblemente ocupada @vanshwadhwa24 la tomó hoy. Abiertobug
openwisp/openwisp-controller#1577 · 3 comentarios · 1 asignado ·
Los mantenedores suelen responder en 1 día
Todos los issues de openwisp/openwisp-controller
Issues similares
-
fidelity
Dificultad 2/5 1-3 horas Aptitud para principiantes 68/100
Los mantenedores suelen responder en 1 día
-
backlog
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
sagemathinc/sagebrush#3434 ·
Los mantenedores suelen responder en 1 día
-
marketing
Dificultad 2/5 1-3 horas Aptitud para principiantes 85/100
Los mantenedores suelen responder en 1 día
-
documentation good first issue help wanted
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
wuisabel-gif/MemWhale#576 ·
Los mantenedores suelen responder en 1 día
-
Status: Triage Suggestion
Dificultad 2/5 1-3 horas Aptitud para principiantes 68/100
GTNewHorizons/GT-New-Horizons-Modpack#27957 ·
Los mantenedores suelen responder en 1 día