Flagsmith/flagsmith

AuditLog creation fails when a segment override is deleted

Opened this issue · 1 comments

How are you running Flagsmith

  • Self Hosted with Docker
  • Self Hosted with Kubernetes
  • SaaS at flagsmith.com
  • Some other way (add details in description below)

Describe the bug

AuditLog (required for webhooks) is not created when a segment override (FeatureStateValue) is deleted

Attaching api server logs when this happens

[12/Nov/2024 12:39:28] "GET /api/v1/features/feature-segments/?environment=1&feature=2 HTTP/1.1" 200 52
FeatureSegment for second_button with priority 0
Exception in thread Thread-8 (create_audit_log_from_historical_record):
Traceback (most recent call last):
  File "/opt/miniconda3/envs/flagsmith/lib/python3.12/site-packages/django/db/models/fields/related_descriptors.py", line 218, in __get__
    rel_obj = self.field.get_cached_value(instance)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/miniconda3/envs/flagsmith/lib/python3.12/site-packages/django/db/models/fields/mixins.py", line 15, in get_cached_value
Project proj p1 - Environment Development - Feature second_button - Enabled: True
    return instance._state.fields_cache[cache_name]
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^
KeyError: 'feature_state'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/opt/miniconda3/envs/flagsmith/lib/python3.12/threading.py", line 1073, in _bootstrap_inner
    self.run()
  File "/opt/miniconda3/envs/flagsmith/lib/python3.12/threading.py", line 1010, in run
    self._target(*self._args, **self._kwargs)
  File "/Users/instinct/Desktop/flagsmith/api/audit/tasks.py", line 94, in create_audit_log_from_historical_record
[12/Nov/2024 12:39:28] "GET /api/v1/features/featurestates/?environment=1&feature=2 HTTP/1.1" 200 1057
    environment, project = instance.get_environment_and_project()
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/instinct/Desktop/flagsmith/api/core/models.py", line 141, in get_environment_and_project
    environment, project = self._get_environment(), self._get_project()
                           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/instinct/Desktop/flagsmith/api/features/models.py", line 1136, in _get_environment
    return self.feature_state.environment
           ^^^^^^^^^^^^^^^^^^
  File "/opt/miniconda3/envs/flagsmith/lib/python3.12/site-packages/django/db/models/fields/related_descriptors.py", line 236, in __get__
    rel_obj = self.get_object(instance)
              ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/miniconda3/envs/flagsmith/lib/python3.12/site-packages/django/db/models/fields/related_descriptors.py", line 366, in get_object
    return super().get_object(instance)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/miniconda3/envs/flagsmith/lib/python3.12/site-packages/django/db/models/fields/related_descriptors.py", line 199, in get_object
    return qs.get(self.field.get_reverse_related_filter(instance))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/miniconda3/envs/flagsmith/lib/python3.12/site-packages/django/db/models/query.py", line 637, in get
    raise self.model.DoesNotExist(
features.models.FeatureState.DoesNotExist: FeatureState matching query does not exist.
[12/Nov/2024 12:39:28] "GET /api/v1/projects/1/features/2/ HTTP/1.1" 200 659

Steps To Reproduce

  1. Start the python api on a terminal to see the logs
  2. Open the a feature's settings and go to the Settings Overrides tab
  3. (if already a sample segment is present, you can skip this step) Create a new segment and click on "Update Segment Overrides"
  4. Delete the segment and again click on "Update Segment Overrides"
  5. Observe the api terminal and check the thrown error (same as the error pasted above)

Installation

  • local setup by cloning the repo and running the latest flagsmith api server and client on git main branch

Expected behavior

No errors to be thrown on the api server.
Audit Log should get created.

Screenshots

No response

Hi @uinstinct , I've been unable to reproduce this issue. As you can see from the following screenshot, the audit log record is correctly created when a segment override is deleted.

Image