Skip to content

fix: prevent deadlock when deleting user with orphan organisations - #8837

Open
srijantrpth wants to merge 3 commits into
Flagsmith:mainfrom
srijantrpth:fix-user-delete-deadlock
Open

srijantrpth wants to merge 3 commits into
Flagsmith:mainfrom
srijantrpth:fix-user-delete-deadlock

Conversation

@srijantrpth

Copy link
Copy Markdown
Contributor

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

Closes #8775

This PR fixes a Postgres deadlock that occurs when deleting a user who is the sole member of an organisation.

Previously, FFAdminUser.delete() executed in two separate transactions, causing a race condition where the task processor's background cascade-delete tasks would attempt to lock the same rows in reverse order. This PR wraps both FFAdminUser.delete() and the e2e teardown function delete_user_and_its_organisations() in transaction.atomic() context managers to ensure the deletion commits fully before background tasks are queued.

How did you test this code?

  • Ran local backend test suites (make test opts="tests/unit/users/") to ensure user deletion logic still passes.
  • Verified the transaction.atomic() syntax and logic successfully bypasses the race condition documented in the issue.

Copilot AI balanced review requested due to automatic review settings October 23, 2026 07:41
@srijantrpth
srijantrpth requested a review from a team as a code owner October 23, 2026 07:41
@srijantrpth
srijantrpth requested review from khvn26 and removed request for a team October 23, 2026 07:41
@vercel

vercel Bot commented Oct 23, 2026

Copy link
Copy Markdown

@srijantrpth 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 reached their quota limit.

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 Oct 23, 2026
@coderabbitai

coderabbitai Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
📝 Walkthrough

The e2e teardown helper and FFAdminUser.delete() now perform deletion work inside database transactions. The Project.handle_cascade_delete hook runs after the transaction commits. Project tests patch transaction.on_commit to execute callbacks immediately.

Priority: ⚪ Pending latest changes

Estimated code review effort: 2 (Simple) | ~10 minutes

Severity of issue fixed: Medium

Merge Risk: 🟡 Moderate · up to 19677

Cascade tasks can be lost after deletion commits, and a changed project test fails before exercising its request; fix these before merging.

✨ Finishing Touches: 💡 1
🛠️ Fix failing CI checks: 💡
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

@codecov

codecov Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.98%. Comparing base (c582d5b) to head (293ecab).
⚠️ Report is 8 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #8837      +/-   ##
==========================================
- Coverage   98.88%   97.98%   -0.90%     
==========================================
  Files        1683     1683              
  Lines       70275    70273       -2     
==========================================
- Hits        69489    68860     -629     
- Misses        786     1413     +627     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@coderabbitai coderabbitai 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.

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: 38ce1a3c-73db-439d-b6bb-22c85be1cc41
📥 Commits

Reviewing files that changed from the base of the PR and between d031dbe and 6798d5c.

📒 Files selected for processing: 2
  • api/e2etests/e2e_seed_data.py
  • api/users/models.py

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

Comment thread api/users/models.py

@coderabbitai coderabbitai 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.

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: 4fc5ac09-0c24-48c0-a351-4b6b8b27c137
📥 Commits

Reviewing files that changed from the base of the PR and between 6798d5c and 1d75049.

📒 Files selected for processing: 1
  • api/projects/models.py

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 6 remain after this review.

Comment thread api/projects/models.py
@srijantrpth
srijantrpth force-pushed the fix-user-delete-deadlock branch 2 times, most recently from 432b261 to d2f91f9 Compare October 23, 2026 08:24

@coderabbitai coderabbitai 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.

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: 29a199fc-2b05-4e6a-816c-25d5e6ece5b6
📥 Commits

Reviewing files that changed from the base of the PR and between 1d75049 and 432b261.

📒 Files selected for processing: 1
  • api/tests/unit/projects/test_unit_projects_views.py

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 5 remain after this review.

Comment thread api/tests/unit/projects/test_unit_projects_views.py Outdated
@srijantrpth
srijantrpth force-pushed the fix-user-delete-deadlock branch 3 times, most recently from 054b5bb to 31f5e76 Compare October 23, 2026 08:33

@coderabbitai coderabbitai 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.

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: d6f5d131-d1e6-4603-ab6e-78de8459c03d
📥 Commits

Reviewing files that changed from the base of the PR and between 002a3c5 and 196774f.

📒 Files selected for processing: 4
  • api/e2etests/e2e_seed_data.py
  • api/projects/models.py
  • api/tests/unit/projects/test_unit_projects_views.py
  • api/users/models.py

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

Comment thread api/tests/unit/projects/test_unit_projects_views.py Outdated
@srijantrpth
srijantrpth force-pushed the fix-user-delete-deadlock branch from 293ecab to 1baf14e Compare October 23, 2026 08:41

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.

Deleting a user with orphan organisations races project cascade-delete tasks and can deadlock

2 participants