-
Notifications
You must be signed in to change notification settings - Fork 0
fix: handle duplicate room creation race condition #157
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
base: test
Are you sure you want to change the base?
Conversation
Catch 23505 unique constraint error when frontend creates room before backend's selectRoom sees it. Skip notification if room already exists.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Braintrust eval reportCatalog Opportunity Analysis Evaluation (HEAD-1769483992)
Catalog Songs Count Evaluation (HEAD-1769483992)
First Week Album Sales Evaluation (HEAD-1769483992)
Memory & Storage Tools Evaluation (HEAD-1769483992)
Monthly Listeners Tracking Evaluation (HEAD-1769483992)
Search Web Tool Evaluation (HEAD-1769483992)
Social Scraping Evaluation (HEAD-1769483992)
Spotify Followers Evaluation (HEAD-1769483992)
Spotify Tools Evaluation (HEAD-1769483992)
TikTok Analytics Questions Evaluation (HEAD-1769483992)
|
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.
why are you removing the promise.all?
Simpler fix - keep parallel execution, only catch the 23505 error.
This is the actual fix - createNewRoom is called during setupConversation at request START, which is where the 500 error occurs.
Frontend no longer creates rooms, so no race condition. Backend is single source of truth for room creation.
- Changed insertRoom to use upsert with ignoreDuplicates: true - Reverted try/catch in createNewRoom.ts and handleChatCompletion.ts - Cleaner solution: duplicates are silently ignored at the DB level
One-line fix: change insert to upsert in insertRoom.ts. Removes try/catch workarounds since upsert handles duplicates.
Problem
Intermittent 500 errors on new chat due to
duplicate key value violates unique constraint "rooms_pkey".Root Cause
Frontend and backend both try to create the same room:
Recoup-Chat/app/api/chat/create/route.ts→ Creates room for optimistic UIRecoup-API/lib/chat/handleChatCompletion.ts→ Also creates room (for API clients)Race condition: backend's
selectRoom()returns null before frontend's insert is visible → duplicate key error.Solution
Catch the 23505 (unique constraint violation) error in
handleChatCompletion. If room already exists, skip notification and continue - the frontend already created it.Changes
insertRoomin try/catchRelated
refetchConversations()toonFinishto sync title updates