Skip to content

fix(Audit logs): Skip creation messages for segment overrides already deleted - #9171

Open
PDGamerSG wants to merge 2 commits into
Flagsmith:mainfrom
PDGamerSG:fix/audit-deleted-segment-override
Open

PDGamerSG wants to merge 2 commits into
Flagsmith:mainfrom
PDGamerSG:fix/audit-deleted-segment-override

Conversation

@PDGamerSG

Copy link
Copy Markdown

Thanks for submitting a PR! Please check the boxes below:

  • I have read the Contributing Guide.
  • I have added information to docs/ if required so people know about the feature.
  • I have filled in the "Changes" section below.
  • I have filled in the "How did you test this code" section below.

Changes

Follow-up to #6792, which fixed the same error for update messages.

create_audit_log_from_historical_record builds the creation message for a segment override from the live FeatureSegment. If the override is deleted before the task runs, the cascade has already removed the FeatureSegment. The task then raises FeatureSegment.DoesNotExist and fails on all three attempts:

File "/app/features/models.py", line 1025, in get_create_log_message
  return audit_helpers.get_segment_override_created_audit_message(self)
File "/app/features/audit_helpers.py", line 38, in get_segment_override_created_audit_message
  args = (feature_state.feature.name, feature_state.feature_segment.segment.name)
features.models.FeatureSegment.DoesNotExist: FeatureSegment matching query does not exist.

We hit this on self-hosted 2.295.2 when an automated client created a segment override and deleted it a few seconds later. Six audit tasks failed this way in one run.

This PR handles the creation path the same way #6792 handled the update path. FeatureState.get_create_log_message now returns None when the FeatureSegment is gone, so the task exits without an audit log instead of failing and being retried. The deletion of the override is still logged from the FeatureSegment history.

No docs change is needed.

How did you test this code?

  • Added test_create_audit_log_from_historical_record__segment_override_deleted_before_create_task__does_nothing. It creates a segment override that differs from the environment default, so a creation message is due, then deletes the FeatureSegment before running the task on the + historical record. Before the change, the test raised FeatureSegment.DoesNotExist. It now passes.
  • pytest tests/unit/audit tests/unit/features/test_unit_features_audit_helpers.py tests/unit/features/test_unit_features_models.py: 146 passed.
  • ruff check, ruff format --check and mypy pass on both changed files.

… deleted

The task that builds an audit log from a historical feature state reads
the live FeatureSegment to name the segment in a creation message. When
the segment override is deleted before the task runs, the cascade has
already removed the FeatureSegment, so the task raises
FeatureSegment.DoesNotExist and fails on every retry.

Flagsmith#6792 handled this for update messages. Handle the creation path the
same way, so the task returns without an audit log instead of failing.
The deletion of the override is still logged from the FeatureSegment
history.
@PDGamerSG
PDGamerSG requested a review from a team as a code owner November 28, 2026 15:36
@PDGamerSG
PDGamerSG requested review from khvn26 and a balanced review from Copilot and removed request for a team November 28, 2026 15:36
@vercel

vercel Bot commented Nov 28, 2026

Copy link
Copy Markdown

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

A member of the Team first needs to authorize it.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

@github-actions github-actions Bot added the api Issue related to the REST API label Nov 28, 2026

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

No issues found across 2 files

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

Requires human review: Auto-approval skipped because this PR is from an external contributor.

Re-trigger cubic

@Zaimwa9 Zaimwa9 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for the contribution, just one nit to remove an expendable comment

Comment thread api/features/models.py Resolved
@Zaimwa9
Zaimwa9 removed the request for review from khvn26 November 30, 2026 14:26
@Zaimwa9 Zaimwa9 self-assigned this Nov 30, 2026
Co-authored-by: Zaimwa9 <wadii.zaim@gmail.com>

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

0 issues found across 1 file (changes from recent commits).

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

Requires human review: Auto-approval skipped because this PR is from an external contributor.

Re-trigger cubic

@PDGamerSG

Copy link
Copy Markdown
Author

Thanks @Zaimwa9 , I have removed the comment sir and you can have a look now

This branch has not been deployed

No deployments
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants