Repository navigation
fix(profiles): enforce the household profile limit inside the creating transaction - #2577
Conversation
Silo Kody — review completeReview finished. Check the inline comments for findings and verify each suggestion against the code and tests. Reviewing changes in Silo
Review settingsReview OptionsThe following review options are enabled or disabled:
|
|
Concurrent profile creation now returns Suggested fix: No diff — add the API response evidence under Automated check: Macroscope check run agent (gpt-6-luna). Evidence was not reviewed for correctness. Posted via Macroscope — Visible change evidence |
|
Warning Review limit reachedYou'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 15 minutes. View limit details
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info
📝 Walkthrough
Merge Risk: ⚪ Minimal · up to
|
af7f2d6 to
eaa23f9
Compare
Silo Kody — review completeReview finished. Check the inline comments for findings and verify each suggestion against the code and tests. Reviewing changes in Silo
Review settingsReview OptionsThe following review options are enabled or disabled:
|
|
Added before-and-after excerpts to the Evidence section for this case. An account with a limit of 2 and one existing profile gets two |
Quick104
left a comment
There was a problem hiding this comment.
The fix is correct, and the review found nothing that blocks merge. The recount runs after the per-account advisory lock is taken and reads through the same transaction, so concurrent creates on one account serialize across replicas. The sentinel error also survives the transaction wrapper.
Two small items, both of which I'm fixing in a commit on this branch, so there's no need to change anything yourself:
- Merge conflict with main (blocks merge as it stands). #2817 removed
TestHandleCreateProfile_EnforcesUserProfileLimitand its neighbours fromprofiles_test.go, andscripts/ci/db-contracts.txtgained new entries at the same spot. I'm rebasing onto main. The resolution keeps main's removals and this PR's SQLite recount test. - Redundant DB test (inline below).
eaa23f9 to
5d01b05
Compare
Review summaryReviewed head: 5d01b05 (eaa23f9 rebased onto main, plus one test-only commit). The branch was rewritten by a rebase. The fix is correct and ready to merge. The recount runs inside Addressed
Verification
Remaining for maintainers: none. Validated features: no passed case is affected. Fit: 9/10 · Readiness: 9/10 — ready (assuming CI is green)
|
Silo Kody — review completeReview finished. Check the inline comments for findings and verify each suggestion against the code and tests. Reviewing changes in Silo
Review settingsReview OptionsThe following review options are enabled or disabled:
|
…ng transaction Profile creation compared the account's profile count with its limit before opening the write transaction, so concurrent creates on an account one below its limit could all pass and all commit. The count is now repeated inside the per-account transaction that inserts the profile, which Postgres serializes with an advisory lock across replicas and SQLite with BEGIN IMMEDIATE. A full household still answers 409 profile_limit_reached. Closes Silo-Server#2423
TestCreateProfileLimitConcurrentDB already creates to limit - 1, checks that the losing requests get 409 profile_limit_reached, and checks that the household ends at its limit. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Rebased onto current main to clear a conflict in |
5d01b05 to
1c428dc
Compare
Silo Kody — review completeReview finished. Check the inline comments for findings and verify each suggestion against the code and tests. Reviewing changes in Silo
Review settingsReview OptionsThe following review options are enabled or disabled:
|
Problem
Closes #2423
Related issue: N/A
Validation tasks: none
Two profile creations arriving at the same time on an account one below its household profile limit could both succeed, leaving the account over its limit. Profile creation counted the household before opening the write transaction. Nothing re-counted inside the transaction and no database constraint bounds profiles per account, so concurrent requests all passed the same stale count. This applies to a limit set on the account and to one inherited from its access group (#1801).
This change repeats the count inside the transaction that inserts the profile. A household that is already full gets the same
409 profile_limit_reachedit gets today.Approach
createProfileWithSettingsSyncalready runs the profile insert in the per-account preference-settings transaction. On Postgres that transaction holds a per-account advisory lock (pg_advisory_xact_lock), so it serializes creates on one account across every API replica; on SQLite it isBEGIN IMMEDIATE. The handler now passes the account's effective limit in, and the transaction counts the household withListProfileIDsbefore inserting. No schema or store interface change was needed.The existing check before the transaction stays. It keeps answering a full household before
name_conflict, so error precedence for a single request does not change. v1POST /profilesand v2createProfileshare this code path. jellycompat does not create profiles, and account provisioning only creates a new account's first profile.Duplicate names under concurrency are not covered here; #343 tracks that.
Validation
409 profile_limit_reached. Onmainthe concurrent test failed every run, with 5 to 12 of the 12 creates committing. With the fix it passed 20 runs under-race.scripts/ci/db-contracts.txtso the DB contracts job runs it.make test-db-contractspassed against a freshly migrated database.go build,gofmt,go vet,make lint-changed, and the generated-contract, fixture, route, migration-ledger, and docs-hygiene checks passed. Web lint, format, build, and budget passed.make test-go: everything passed except two jellycompat Dolby Vision strip tests, which fail the same way on unmodifiedmainon the test machine.mainCI is green at that commit.make test-web: a different set of 12 to 19 tests failed on each run on a heavily loaded machine. This branch changes no web files.verify-actionlintandverify-shellcheckwere not run because the tools are not installed locally. This branch changes no workflow or shell files.Evidence
Evidence: https://evidence.siloserver.org/r/silo-server/pr-2577/
Native API,
POST /api/v1/profiles(v2createProfileshares the code path). An account withmax_profiles: 2and one existing profile receives two create requests at the same moment. Each build ran against its own fresh local database. Profile IDs are omitted.Before,
mainat df107b7. Both creates succeed and the household ends one over its limit. All 6 runs ended this way:After, this branch at eaa23f9. One create succeeds and the other gets the existing 409. All 10 runs ended this way:
Risks
Each create now takes one extra
SELECT id FROM user_profiles WHERE user_id = $1inside a transaction that already holds the account's lock. Concurrent creates on the same account wait for each other, as they already did for the settings write.Checklist
AI Disclosure
Note
Fix profile-limit enforcement to recheck household size inside the creation transaction
ProfileHandler.createProfileWithSettingsSync, closing a check-then-write race where concurrent requests could exceed the household limit (profiles_settings_sync.go).errProfileLimitReachedsentinel; the handler translates it into a profile-limit conflict response (HTTP 409) instead of an internal error (profiles.go).TestCreateProfileLimitConcurrentDB) with 12 parallel requests on a household one profile below its limit, requiring exactly one success, plus a SQLite unit test and a CI contract-list entry (profiles_limit_db_test.go).Macroscope summarized 1c428dc.