Skip to content

fix(api): handle DoesNotExist in audit log signals and webhooks#6938

Open
wavebyrd wants to merge 1 commit intoFlagsmith:mainfrom
wavebyrd:fix/6879-audit-log-does-not-exist
Open

fix(api): handle DoesNotExist in audit log signals and webhooks#6938
wavebyrd wants to merge 1 commit intoFlagsmith:mainfrom
wavebyrd:fix/6879-audit-log-does-not-exist

Conversation

@wavebyrd
Copy link

@wavebyrd wavebyrd commented Mar 12, 2026

Summary

Fixes #6879

When a FeatureState or FeatureSegment is deleted before the async create_audit_log_from_historical_record task runs, three locations crash with DoesNotExist errors (reported via Sentry as FLAGSMITH-API-5JA, 5JB, 5JD).

This handles DoesNotExist gracefully in all three locations:

  • audit/signals.py - send_feature_flag_went_live_signal: catches FeatureState.DoesNotExist when accessing audited_instance.feature_state and skips the signal
  • integrations/grafana/mappers.py - _get_instance_tags_from_audit_log_record: catches ObjectDoesNotExist and returns empty tags (annotation is still created, just without feature/segment tags)
  • features/tasks.py - _get_feature_state_webhook_data: catches ObjectDoesNotExist when accessing feature_state.feature_segment and falls back to None

Changes

File Change
api/audit/signals.py Wrap FK access in try/except, log and skip
api/integrations/grafana/mappers.py Wrap tag-building in try/except, return []
api/features/tasks.py Extract FK access with try/except, fall back to None
api/tests/unit/audit/test_unit_audit_signals.py Test: deleted FeatureState skips signal
api/tests/unit/integrations/grafana/test_mappers.py Test: deleted FeatureState returns no instance tags
api/tests/unit/features/test_unit_features_tasks.py Test: deleted FeatureSegment still returns webhook data

Test plan

  • test_send_feature_flag_went_live_signal__deleted_feature_state__skips_signal
  • test_map_audit_log_record_to_grafana_annotation__deleted_feature_state__returns_no_instance_tags
  • test_get_feature_state_webhook_data__deleted_feature_segment__returns_data
  • CI passes existing test suite

@wavebyrd wavebyrd requested a review from a team as a code owner March 12, 2026 20:19
@wavebyrd wavebyrd requested review from gagantrivedi and removed request for a team March 12, 2026 20:19
@vercel
Copy link

vercel bot commented Mar 12, 2026

Someone is attempting to deploy a commit to the Flagsmith Team on Vercel.

A member of the Team first needs to authorize it.

@github-actions github-actions bot added the api Issue related to the REST API label Mar 12, 2026
@wavebyrd wavebyrd force-pushed the fix/6879-audit-log-does-not-exist branch 2 times, most recently from 34c9c6d to bb99b2b Compare March 13, 2026 21:23
@wavebyrd wavebyrd requested review from a team as code owners March 13, 2026 21:23
@wavebyrd wavebyrd requested review from talissoncosta and removed request for a team March 13, 2026 21:23
@github-actions github-actions bot added front-end Issue related to the React Front End Dashboard docs Documentation updates infrastructure labels Mar 13, 2026
@matthewelwell
Copy link
Contributor

Hi @wavebyrd , similar to your other PR here, I think you've made a mistake in the merge or rebase as it's showing 3000+ files changed.

…smith#6879)

When a related FeatureState or FeatureSegment is deleted before the
async create_audit_log_from_historical_record task runs, the signals
and mappers crash with DoesNotExist errors. Handle these gracefully:

- audit/signals.py: catch FeatureState.DoesNotExist in
  send_feature_flag_went_live_signal and skip the signal
- integrations/grafana/mappers.py: catch ObjectDoesNotExist in
  _get_instance_tags_from_audit_log_record and return empty tags
- features/tasks.py: catch ObjectDoesNotExist when accessing
  feature_state.feature_segment and fall back to None
@wavebyrd
Copy link
Author

Fixed the rebase - now showing only the single commit for this fix. Apologies for the messy history!

@wavebyrd wavebyrd force-pushed the fix/6879-audit-log-does-not-exist branch from bb99b2b to b01891f Compare March 16, 2026 13:16
Copy link
Contributor

@emyller emyller left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I understand these changes, they will eliminate the error by suppressing it.

Similarly to this other pull request, the solution could be suppressing it, but it could also be avoiding it.

Either way, I think this needs deeper investigation, as did the pull request linked above, in order to make sure we're fixing the problem correctly, and not hiding others silently.

@wavebyrd Again, thanks for your contribution. Allow us time to investigate what needs to be done here, or feel free to take another stab with this review in mind — though I think it needs effort to reproduce and investigate the user journey in detail.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api Issue related to the REST API docs Documentation updates front-end Issue related to the React Front End Dashboard infrastructure

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DoesNotExist errors in audit log signals when related objects are deleted

3 participants