Skip to content

fix(Segments): delete_segment task crashes when segment already deleted - #8136

Open
bardock-2393 wants to merge 1 commit into
Flagsmith:mainfrom
bardock-2393:fix/segment-delete-task-race-condition
Open

fix(Segments): delete_segment task crashes when segment already deleted#8136
bardock-2393 wants to merge 1 commit into
Flagsmith:mainfrom
bardock-2393:fix/segment-delete-task-race-condition

Conversation

@bardock-2393

Copy link
Copy Markdown

The async delete_segment task fetches a segment by id and deletes it, but a concurrent delete of the same segment (e.g. a cascading project or feature delete racing with another delete of the same segment) can remove it first. The task then crashes with Segment.DoesNotExist instead of treating the segment as already gone.

Changes

  • delete_segment task no longer crashes when the segment was already removed by a concurrent delete
  • Added regression tests covering both the normal delete and the already-deleted case

Closes #7354

Review effort: 1/5

The async delete_segment task fetches the segment by id before
deleting it, but concurrent execution (e.g. a cascading feature or
project delete racing with a prior run of the same task) can remove
the row first, causing Segment.DoesNotExist to crash the task.
@bardock-2393
bardock-2393 requested a review from a team as a code owner July 28, 2026 13:53
@bardock-2393
bardock-2393 requested review from emyller and removed request for a team July 28, 2026 13:53
@vercel

vercel Bot commented Jul 28, 2026

Copy link
Copy Markdown

@bardock-2393 is attempting to deploy a commit to the Flagsmith Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai

coderabbitai Bot commented Jul 28, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 394881b5-0cb1-4a32-b5d4-5ac04cfb22ad

📥 Commits

Reviewing files that changed from the base of the PR and between 1201efd and d156f71.

📒 Files selected for processing (2)
  • api/segments/tasks.py
  • api/tests/unit/segments/test_unit_segments_tasks.py

📝 Walkthrough

Walkthrough

The segment deletion task now catches Segment.DoesNotExist and treats missing segments as a no-op. Unit tests cover soft deletion of an existing segment and repeated invocation after hard deletion.

Estimated code review effort: 1 (Trivial) | ~5 minutes


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@github-actions github-actions Bot added the api Issue related to the REST API label Jul 28, 2026
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.

Race condition in deleting segments

1 participant