fix(contacts): make first-contact upserts race-safe - #12
Merged
Conversation
This was referenced Aug 27, 2026
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.
Concurrent
/v1/trackrequests can all observe a missing normalized contact, then race oncontact.create. The unique(projectId, email)constraint elects one row, but the losing requests currently surface Prisma P2002 as 500s and drop their events.This change treats P2002 as a successful election: each loser reads and continues with the elected contact. It does not replay stale persistent data or a default subscription state over the winner. Explicit opt-outs remain monotonic through a compare-and-set update, and only the request that actually changes subscription state emits the subscription event.
The gate evidence now forces eight parallel service upserts through the same missing-row observation and sends a twelve-request HTTP burst. The burst asserts twelve 200 responses, one normalized contact, twelve distinct durable events, and at most one active execution while the workflow is waiting. Both contact-winner orderings and stale concurrent unsubscribe observers are covered; the existing normalization and cross-project isolation suite remains green.
Verification:
yarn lint— passed (0 errors; existing warnings only)yarn workspace api build— passedyarn test:run— 1,178/1,180 passed; two unrelated performance gates missed locally (49,909 vs 50,000 renders/s, and the existing 10k-contact benchmark timed out at 120s)yarn build— API and preceding packages passed; the unchanged landing workspace stopped the monorepo build withCould not parse output from TypeScript's --showConfigReview note: two local concurrency review passes found an opt-out ordering edge and duplicate subscription-event emission from stale observers. Both are fixed and have dedicated regressions in this PR.
Portable upstream counterpart: useplunk#469
Closes #10