From af32b759b552f0b0fe2b7fe5a276484d2e3002cf Mon Sep 17 00:00:00 2001 From: Tim McCormack Date: Fri, 3 Nov 2023 21:27:57 +0000 Subject: [PATCH] fix: ConfigWatcher needs to register receivers using strong references These inner functions would otherwise get GC'd right away, since Django only holds weak references to receivers by default. (I don't know why this worked in devstack.) --- CHANGELOG.rst | 6 ++++++ edx_arch_experiments/__init__.py | 2 +- .../config_watcher/signals/receivers.py | 11 +++++++++-- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 6c447b5..34e9765 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -14,6 +14,12 @@ Change Log Unreleased ~~~~~~~~~~ +[3.1.1] - 2023-11-06 +~~~~~~~~~~~~~~~~~~~~ +Fixed +_____ +* ConfigWatcher should now respond to model events properly now that it registers receivers with strong references. (Tested in sandbox.) + [3.1.0] - 2023-10-31 ~~~~~~~~~~~~~~~~~~~~ diff --git a/edx_arch_experiments/__init__.py b/edx_arch_experiments/__init__.py index 1d15ee0..2bf91e3 100644 --- a/edx_arch_experiments/__init__.py +++ b/edx_arch_experiments/__init__.py @@ -2,4 +2,4 @@ A plugin to include applications under development by the architecture team at 2U. """ -__version__ = '3.1.0' +__version__ = '3.1.1' diff --git a/edx_arch_experiments/config_watcher/signals/receivers.py b/edx_arch_experiments/config_watcher/signals/receivers.py index c03f2bc..e9ed4c9 100644 --- a/edx_arch_experiments/config_watcher/signals/receivers.py +++ b/edx_arch_experiments/config_watcher/signals/receivers.py @@ -99,7 +99,14 @@ def _register_waffle_observation(*, model, short_name, fields): short_name (str): A short descriptive name for an instance of the model, e.g. "flag" fields (list): Names of fields to report on in the Slack message """ - @receiver(signals.post_save, sender=model, dispatch_uid=f"config_watcher_{short_name}_change") + + # Note that weak=False is required here. Django by default only + # holds weak references to receiver functions. But these inner + # functions would then be garbage-collected, and Django would drop + # them. So pass weak=False to make Django hold strong references + # instead. (For some reason, this isn't needed in devstack...) + + @receiver(signals.post_save, sender=model, weak=False, dispatch_uid=f"config_watcher_{short_name}_change") def report_waffle_change(*args, instance, created, **kwargs): try: _report_waffle_change(short_name, instance, created, fields) @@ -107,7 +114,7 @@ def report_waffle_change(*args, instance, created, **kwargs): # Log and suppress error so Waffle change can proceed log.exception(f"Failed to report change to waffle {short_name}") - @receiver(signals.post_delete, sender=model, dispatch_uid=f"config_watcher_{short_name}_delete") + @receiver(signals.post_delete, sender=model, weak=False, dispatch_uid=f"config_watcher_{short_name}_delete") def report_waffle_delete(*args, instance, **kwargs): try: _report_waffle_delete(short_name, instance)