Fix lost-update race in access-policy upsertPolicy - #490
Merged
Merged
Conversation
Reconstructs the real-Postgres finding that two concurrent upsertPolicy calls patching different fields of the same tenant's policy can silently revert each other, for both an existing row and a brand-new tenant's first write. A plain Promise.all of two real calls does not reliably force the interleaving on a fast local connection, so the existing-row case uses a third connection that takes the row's lock first and holds it open while both real calls start and queue up behind it, guaranteeing the overlap. Mirrors the existing consumePendingInvite concurrency test in the same file.
upsertPolicy read the current policy row and wrote a merged result back as two separate round trips, with no transaction, row lock, or optimistic-concurrency guard. Two concurrent PATCHes touching different fields of the same tenant's policy could both read the same baseline, so whichever write landed second silently reverted the other's field. Wraps upsertPolicy in a transaction with SELECT ... FOR UPDATE rather than an optimistic version-stamp check: two transactions starting in the same wall-clock tick could still both pass a version check, which is exactly the bug this closes. Because the policy row may not exist yet, an INSERT ... ON CONFLICT DO NOTHING guarantees a row exists before the row lock is taken, serializing concurrent first-writes for a brand-new tenant on the insert's unique index instead of racing them.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
upsertPolicyinpackages/access-policy/src/store.tsread the current tenant policy row and wrote a merged result back as two separate round trips, with no transaction, row lock, or optimistic-concurrency guard. Two concurrent PATCHes touching different fields of the same tenant's policy (e.g. one admin togglesselfSignup, another setsallowedDomains) could both read the same baseline, so whichever write landed second silently reverted the other's field.Fixes CL-7232.
Concurrency mechanism
Wraps
upsertPolicyin a transaction withSELECT ... FOR UPDATErow locking rather than an optimistic version-stamp check. This is the same choice a sibling in-flight lane made for the identical lost-update shape elsewhere in this codebase (a workbench-settings mutate path): an optimistic check based on a wall-clock version stamp was rejected there because two transactions starting in the same tick could still both pass it, which is exactly the bug this PR closes.Because
access_policy.policy's row may not exist yet (this is an upsert, not always an update),SELECT ... FOR UPDATEalone has nothing to lock for a brand-new tenant. AnINSERT ... ON CONFLICT DO NOTHINGguarantees a row exists first: two concurrent first-writes for the same tenant serialize on that insert's unique-index conflict — the loser blocks until the winner's transaction commits, then no-ops and its own subsequentSELECT ... FOR UPDATEsees the winner's committed row.Tests
Two new regression tests in
store.drizzle.test.tsagainst real Postgres:upsertPolicycalls start and queue up behind it, forcing the exact worst-case interleaving (both reads before either write commits) deterministically rather than hoping a fast local round trip races that way.Promise.all, which reliably reproduces the bug for the insert path on a real database.Both were verified to fail against the pre-fix implementation and pass against the fix.
Stack
This is the base of a 3-PR stack: CL-7233 (onboarding map eviction) branches off this, and CL-7234 (onboarding reportError) branches off that. Do not merge out of order.
Review process note
Greybeard reviewed the approach before implementation; Critique reviewed the committed diff and found the original existing-row test did not actually discriminate buggy from fixed code (a
Promise.allwas too fast to force the interleaving) plus two commit-message/comment hygiene issues (an external ticket reference, and citing an uncommitted sibling as settled fact). Both commits were rewritten to address every finding, verified empirically (the fixed test now reliably fails pre-fix and passes post-fix).