Flagsmith / Flagsmith/flagsmith
DoesNotExist errors in audit log signals when related objects are deleted
- Dominant language
- Python
- Stars
- 6.6k
- Forks
- 567
- Avg merge
- 1d 13h
- Merged PRs (30d)
- 121
Description
Multiple Sentry issues report `FeatureState.DoesNotExist` and `FeatureSegment.DoesNotExist` errors in audit log creation tasks. The pattern is: a historical record is created, then the related object is deleted before the async `create_audit_log_from_historical_record` task runs, causing the task to fail when it tries to access the now-deleted related object.
## Sentry Issues
- **FLAGSMITH-API-5JA** (1 occurrence) — `FeatureState.DoesNotExist` in `audit/signals.py:221` (`send_feature_flag_went_live_signal`) — `audited_instance.feature_state` references a deleted `FeatureState`
- **FLAGSMITH-API-5JB** (2 occurrences) — `FeatureState.DoesNotExist` in `integrations/grafana/mappers.py:39` (`_get_instance_tags_from_audit_log_record`) — `instance.feature_state.feature` references a deleted `FeatureState`
- **FLAGSMITH-API-5JD** (2 occurrences) — `FeatureSegment.DoesNotExist` in `features/tasks.py:141` (`_get_feature_state_webhook_data`) — `feature_state.feature_segment` references a deleted `FeatureSegment`
## Root Cause
The `create_audit_log_from_historical_record` task runs asynchronously. By the time it executes, the related `FeatureState` or `FeatureSegment` may have been deleted (e.g. by a concurrent `delete_environment` or `delete_feature` task). The signals/mappers access these related objects without handling the `DoesNotExist` case.
## Suggested Approach
Handle `DoesNotExist` gracefully in:
- `audit/signals.py:221` — `send_feature_flag_went_live_signal`
- `integrations/grafana/mappers.py:39` — `_get_instance_tags_from_audit_log_record`
- `features/tasks.py:141` — `_get_feature_state_webhook_data`
Either skip the audit log/webhook/annotation when the related object no longer exists, or catch `DoesNotExist` and log a warning.
Contributor guide
Assessment
This issue has not been assessed yet.