-
Notifications
You must be signed in to change notification settings - Fork 6.2k
fix prisma session race condition error #3874
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
@kriscarle is attempting to deploy a commit to the umami-software Team on Vercel. A member of the Team first needs to authorize it. |
Greptile OverviewGreptile SummaryReplaced the query-then-update pattern with a more resilient Key Changes:
Confidence Score: 4/5
Important Files ChangedFile Analysis
Sequence DiagramsequenceDiagram
participant Client
participant API as saveSessionData
participant DB as Prisma/Database
Client->>API: saveSessionData(sessionData)
Note over API: Flatten JSON into key-value pairs
API->>API: flattenJSON(sessionData)
API->>API: Generate UUIDs for each record
loop For each flattened data item
API->>DB: updateMany({sessionId, dataKey})
DB-->>API: {count: n}
alt count === 0 (no existing record)
API->>DB: create(newRecord)
DB-->>API: Created record
else count > 0 (record updated)
Note over API: Record already updated, continue
end
end
API-->>Client: Success
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Additional Comments (1)
-
src/queries/sql/sessions/saveSessionData.ts, line 54-69 (link)logic: race condition still possible: if two requests both get
count === 0between lines 54-65, both will executecreateat line 66-68, creating duplicate records with samesessionId+dataKey. without a unique constraint on(sessionId, dataKey)in the database schema or wrapping this in a transaction, duplicates can still occur. consider adding@@unique([sessionId, dataKey])to theSessionDatamodel in schema.prisma, then use Prisma'supsertinstead of this pattern.
1 file reviewed, 1 comment
mikecao
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 Thanks
I'm getting this error in the logs, seems to be related to a race condition, with multiple processes updating the sessions