#1670

fix: lock the org when maintainers step down or leave at the same moment

OpenAkiyo974wants to merge intofrom

The problem

An org must always keep a maintainer. UpdateOrgMemberRequest checks that, but before the action runs, so two maintainers stepping down at the same moment both pass the check.

What happens next on Postgres: each request updates its own participant row, then records the event. CreateMessage::advance() updates every participant row, including the one the other request already holds. They deadlock, and one of them gets a 500 (SQLSTATE[40P01]: Deadlock detected). Two maintainers leaving at the same moment hit the same deadlock in DeleteOrgMember.

When the timing avoids the deadlock, the org ends up with no maintainer at all. After that, nobody can add people or promote anyone. The new test below shows this: without the fix, the second demotion returns 204 and the org is left with zero maintainers.

Reproduced for real

Local setup: Postgres, an org with two maintainers and one contributor, and two processes started at the same instant. Each process runs the request's check, then the action, just like a real request.

demote both at onceboth leave at once
productiondeadlock, 500 for one of them, on every trydeadlock, 500 for one of them, on every try
this branchone succeeds, the other gets "An org needs at least one maintainer." (422)both leave, and the remaining member inherits the org

The fix

This is the same approach UpdateOrganizationMemberRole already takes for organizations, with OrganizationMustHaveAnAdmin:

  • UpdateOrgMemberRole and DeleteOrgMember lock the org row first, as messages, reactions and CreateOrgMembers already do. Everything that touches the participant rows now takes the locks in the same order, so nothing deadlocks.
  • Under that lock, UpdateOrgMemberRole checks the last maintainer again and throws OrgNeedsMaintainer. The web and API controllers turn it into the same 422 the request gives (errors.role).
  • In DeleteOrgMember, the second leave now waits for the first one, so the "no maintainer left, so hand it over" logic sees the real state, and the heir inherits.

Tests

tests/Feature/Controllers/OrgMemberControllerTest.php and Api/OrgMemberControllerTest.php each get "keeps a maintainer when two step down at the same moment". The test uses beforeResolving(UpdateOrgMemberRole::class) to demote the other maintainer after validation and before the action. It fails on production (204, zero maintainers) and passes here.

Locally: pint, rector, phpstan, pest (6509 passed, 100% coverage) and type coverage (100%) all pass.

Summary by CodeRabbit

  • Bug Fixes
    • Organizations can no longer be left without a maintainer when multiple maintainers try to step down at the same time.
    • Attempts to remove an organization’s last maintainer role now return a validation error explaining that an organization needs at least one maintainer. The change is rejected, and the member remains a maintainer.

Activity

3
copilot-pull-request-reviewer[bot]reviewed9d ago
copilot-pull-request-reviewer[bot]9d ago

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.

coderabbitai[bot]9d ago

No actionable comments were generated in the recent review.

Walkthrough

Organization member deletion and role updates now lock the organization row. Role updates reject a demotion that would leave the organization without another maintainer. Both controllers return a 422 JSON response for this case, and feature tests cover the concurrent demotion scenario.

Changes: Maintainer protection

Layer / File(s)Summary
Serialize organization member changes
app/Exceptions/OrgNeedsMaintainer.php, app/Actions/UpdateOrgMemberRole.php, app/Actions/DeleteOrgMember.php
Role updates lock the organization row and check for another maintainer before demotion. The action throws OrgNeedsMaintainer when none exists. Member deletion also locks the organization row.
Return the maintainer error
app/Http/Controllers/Api/OrgMemberController.php, app/Http/Controllers/OrgMemberController.php
Both controllers catch OrgNeedsMaintainer and return a 422 JSON response with the exception message in the message and role error fields.
Verify concurrent demotion rejection
tests/Feature/Controllers/Api/OrgMemberControllerTest.php, tests/Feature/Controllers/OrgMemberControllerTest.php
Feature tests simulate another maintainer stepping down before the role update. They verify the request is rejected and the current user remains a maintainer.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix

Sequence Diagram(s)

sequenceDiagram
  participant Client
  participant OrgMemberController
  participant UpdateOrgMemberRole
  participant Database
  Client->>OrgMemberController: Submit role update
  OrgMemberController->>UpdateOrgMemberRole: Update member role
  UpdateOrgMemberRole->>Database: Lock organization row
  UpdateOrgMemberRole->>Database: Check for another maintainer
  Database-->>UpdateOrgMemberRole: No other maintainer
  UpdateOrgMemberRole-->>OrgMemberController: Throw OrgNeedsMaintainer
  OrgMemberController-->>Client: Return 422 role error

Suggested reviewers: xheaven, nunomaduro

Merge Risk: Minimal · up to 0e988

Concurrent maintainer demotions and departures are now serialized on the organization row, so an org should not end up without a maintainer. No concrete merge-blocking defect was found in the reviewed changes.

Pre-merge checks: 4

Check nameStatusExplanation
Description CheckCheck skipped - CodeRabbit’s high-level summary is enabled.
Title checkThe title names the main change: the org is now locked when maintainers step down or leave at the same moment. The changeset adds a lock on the organization row in UpdateOrgMemberRole and DeleteOrgMem…
Linked Issues checkCheck skipped because no linked issues were found for this pull request.
Out of Scope Changes checkCheck skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: the org is now locked when maintainers step down or leave at the same moment. The changeset adds a lock on the organization row in UpdateOrgMemberRole and DeleteOrgMember, re-checks for a remaining maintainer under that lock, and adds an OrgNeedsMaintainer exception with a 422 mapping in both controllers. The title matches the objectives and the summaries.

Finishing Touches

Generate unit tests (beta)

  • 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

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: a3196b69-8320-4f4c-bad9-e11f6ff787dc
Commits

Reviewing files that changed from the base of the PR and between 0e09618a7765cf6f92a53b6ddb9a1a79e49c46b6 and 0e98827a53c7af710ebb15811b5a8148f12aeee5.

Files selected for processing: 7

  • app/Actions/DeleteOrgMember.php
  • app/Actions/UpdateOrgMemberRole.php
  • app/Exceptions/OrgNeedsMaintainer.php
  • app/Http/Controllers/Api/OrgMemberController.php
  • app/Http/Controllers/OrgMemberController.php
  • tests/Feature/Controllers/Api/OrgMemberControllerTest.php
  • tests/Feature/Controllers/OrgMemberControllerTest.php

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