fix(storage): stop workspace ledger locks from deadlocking on FK key-share - #7187
Merged
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub. |
Contributor
Greptile SummaryThe PR replaces unnecessarily strong PostgreSQL workspace and payer ledger locks with
Confidence Score: 5/5The PR appears safe to merge. No blocking failure remains.
|
| Filename | Overview |
|---|---|
| apps/sim/lib/billing/storage/tracking.ts | Changes workspace and payer ledger locks to FOR NO KEY UPDATE while retaining consistent workspace-before-payer ordering and adds rationale for the lock contract. |
| apps/sim/lib/billing/storage/payer-transfer.ts | Uses FOR NO KEY UPDATE for ordered workspace and payer locks without changing payer-transfer calculations or mutation ordering. |
| apps/sim/lib/billing/storage/tracking.test.ts | Adds regression coverage asserting the weaker lock mode for both user and organization payers in individual and batched mutations. |
| apps/sim/lib/workspaces/organization-workspaces.ts | Downgrades workspace-row locks used during organization attachment and payer transitions while retaining mutual exclusion among row writers. |
| apps/sim/lib/workspaces/permissions/utils.ts | Updates the optional transactional workspace lock used by permission mutations to FOR NO KEY UPDATE. |
Sequence Diagram
sequenceDiagram
participant U1 as Upload transaction A
participant U2 as Upload transaction B
participant C as Billable child rows
participant W as Workspace ledger
participant P as Payer ledger
U1->>C: Insert billable row
U2->>C: Insert billable row
Note over U1,U2: Each transaction holds KEY SHARE on parent rows
U1->>W: FOR NO KEY UPDATE
U1->>P: FOR NO KEY UPDATE
Note over U2,W: Waits for transaction A without a KEY SHARE lock upgrade
U1->>W: Update storage counter
U1->>P: Update payer counter
U1-->>U2: Commit and release locks
U2->>W: Acquire FOR NO KEY UPDATE
U2->>P: Acquire FOR NO KEY UPDATE
U2->>W: Update storage counter
U2->>P: Update payer counter
Reviews (3): Last reviewed commit: "docs(table): correct the stale FOR UPDAT..." | Re-trigger Greptile
There was a problem hiding this comment.
1 issue found across 11 files
Confidence score: 4/5
- In
apps/sim/lib/billing/storage/tracking.test.ts, both regression tests useORG_CONTEXT, so they verify only workspace and organization locks while leaving theuserStatspayer lock unchecked; a lock-mode regression there could pass unnoticed. Add coverage using the appropriate user-stats payer context.
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="apps/sim/lib/billing/storage/tracking.test.ts">
<violation number="1" location="apps/sim/lib/billing/storage/tracking.test.ts:219">
P2: The new regression tests claim to assert every storage ledger lock is FOR NO KEY UPDATE, but both use ORG_CONTEXT (organization payer), so only the workspace and organization locks are checked. The userStats payer lock (lib/billing/storage/tracking.ts lines 160-163 and 333-337) is never exercised by these assertions, so a revert of only the user-payer lock to FOR UPDATE would still let both new tests pass. Add a test that runs a mutation against a `user` billing entity and asserts mockTxFor/mockTxOrderedFor is called with 'no key update' to close the gap.</violation>
</file>
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
Collaborator
Author
Collaborator
Author
|
@cubic review |
@waleedlatif1 I have started the AI code review. It will take a few minutes to complete. |
…share Workspace storage accounting locked the workspace, organization, and user_stats rows with SELECT ... FOR UPDATE. Those rows are foreign-key parents, so a transaction that has already written a billable child row holds an implicit FOR KEY SHARE on the parent, and the stronger lock is an upgrade that two concurrent uploads take on each other. Take FOR NO KEY UPDATE instead. It does not conflict with FOR KEY SHARE, still conflicts with itself, and is the lock a plain UPDATE of these non-key counters takes anyway, so the ledgers stay serialized.
The lock-mode regression tests only exercised the organization payer, so the user_stats lock branches were never asserted and a revert of just those would have passed. Parameterize both tests over both payer kinds and assert the exact call list, so a lock that stops being taken at all fails too.
The advisory quota check describes createTable's count as the authoritative FOR UPDATE read; that lock is now FOR NO KEY UPDATE.
waleedlatif1
force-pushed
the
fix/workspace-forupdate-deadlock
branch
from
August 28, 2026 01:33
db70ca0 to
737ff44
Compare
Collaborator
Author
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
workspace,organization, anduser_statswithSELECT ... FOR UPDATE. All three are foreign-key parent rows, so a transaction that has already inserted a billable child row (aworkspace_filesrow, in the same transaction) holds an implicitFOR KEY SHAREon the parent. TheFOR UPDATEis then a lock upgrade, and two concurrent uploads in one workspace take it on each other —deadlock detected.FOR NO KEY UPDATE. It does not conflict withFOR KEY SHARE, still conflicts with itself and withFOR UPDATE, and is exactly the lock a plainUPDATEof these non-key counters takes anyway — so the ledgers stay serialized against each other and against payer transfers.workspace-row lock (permissions, credentials, table creation, copilot file materialization, workspace payer moves).FOR UPDATEon a workspace row also blocks every insert into its 49 child tables for the life of the transaction; the weaker lock preserves each site's mutual exclusion without that stall.knowledge_base/knowledge_connector/user_table_definitionsmutation locks alone — different resources, and relaxing those changes what they serialize against.Root cause evidence
Reproduced end-to-end against a local Postgres with the real migrated schema, driving the actual code path:
FOR UPDATEFOR NO KEY UPDATEdeadlock detecteddeadlock detectedThe failing statement is byte-for-byte the one in the error report, and the counter drifts (
100instead of600) because the losing transactions roll back. Also measured the row-lock conflict matrix directly:NO KEY UPDATEconflicts with itself and withFOR UPDATE, but not withKEY SHARE; and a parent heldFOR UPDATEblocks a concurrent child insert, while one heldFOR NO KEY UPDATEdoes not.Type of Change
Testing
no key update; confirmed they fail when the lock strength is revertedbun run type-check,bun run lint,bun run check:audits(37 audits) all passChecklist