#1660

fix: keep an org's member cap and add each person once when maintainers add at the same moment

OpenAkiyo974wants to merge intofrom

The problem

CreateOrgMemberRequest checks who's already in the org and that the org stays within its 256 people. Then CreateOrgMembers inserts the participants without looking again. Two maintainers adding people at the same moment both pass the check:

  • Different people, one place left: both are added, and the org ends up over its cap.
  • The same person: the second insert hits the conversation_participants (conversation_id, user_id) unique index. That maintainer gets a 500, and their whole batch is rolled back.

Reproduced locally with two PHP processes, one per maintainer, each running the request's check and then the action at the same instant, on an org with 255 of its 256 places taken:

Two different newcomersThe same newcomer twice
production (3 runs)257 members, 257, 257UniqueConstraintViolationException in one of the two, every run
With this fix (3 runs)256: one added, the other refusedBoth succeed, added once

The fix

CreateOrgMembers locks the org's conversation row first, the same row messages, reactions and pins lock, then reads who's in again:

  • Someone who joined in the meantime is skipped, so adding a person twice is a no-op, not a 500.
  • If the batch no longer fits, nothing is added and it throws OrgIsFull. The web and API controllers answer it with the same 422 the request gives, members: An org holds up to 256 people.

Tests

The tests use beforeResolving(CreateOrgMembers::class) to add a participant after the request's check, as the other maintainer's request would:

  • adds someone once when two maintainers add them at the same moment (web): 204, and the person is in the org once.
  • keeps the cap when two maintainers fill the last place at the same moment (web), and the same through the API: 422 with the usual message, and the org stays at 256.

All three fail on production (a 500, and 204 instead of 422). The parallel runs above match.

pint, rector, phpstan: clean. Full suite with --coverage --exactly=100.0: 6510 passed, 100%. Type coverage: 100%.

Activity

3
copilot-pull-request-reviewer[bot]reviewed11d ago
copilot-pull-request-reviewer[bot]11d 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]11d ago

WARNING

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 45 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used all 4 included reviews currently available.

Learn how review limits work.

Review configuration:

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: ebb1f634-01ab-4986-9aa9-92378ee9f676
Commits

Reviewing files that changed from the base of the PR and between 90f528e2143885ab36e93bbb26e755d1f3648d44 and 1a382093f82dc9aaa9d8ec249faf8e00d73eb179.

Files selected for processing: 6

  • app/Actions/CreateOrgMembers.php
  • app/Exceptions/OrgIsFull.php
  • app/Http/Controllers/Api/OrgMemberController.php
  • app/Http/Controllers/OrgMemberController.php
  • tests/Feature/Controllers/Api/OrgMemberControllerTest.php
  • tests/Feature/Controllers/OrgMemberControllerTest.php
  • 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