Skip to content

fix: handle duplicate room creation race condition - #157

Merged
sidneyswift merged 7 commits into
testfrom
fix/room-creation-race-condition
Jan 27, 2026
Merged

fix: handle duplicate room creation race condition#157
sidneyswift merged 7 commits into
testfrom
fix/room-creation-race-condition

Conversation

@sidneyswift

Copy link
Copy Markdown
Contributor

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:

  1. Recoup-Chat/app/api/chat/create/route.ts → Creates room for optimistic UI
  2. Recoup-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

  • Wrap insertRoom in try/catch
  • Catch 23505 error → continue without throwing
  • Only send notification if we successfully created the room

Related

  • Recoup-Chat PR: adds refetchConversations() to onFinish to sync title updates

Catch 23505 unique constraint error when frontend creates room before
backend's selectRoom sees it. Skip notification if room already exists.
@vercel

vercelBot commented Jan 26, 2026

Copy link
Copy Markdown
Contributor

The latest updates on your projects. Learn more about Vercel for GitHub.

ProjectDeploymentReviewUpdated (UTC)
recoup-apiReadyReadyPreviewJan 27, 2026 2:40pm

@coderabbitai

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

  • 🔍 Trigger a full review

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions

github-actionsBot commented Jan 26, 2026

Copy link
Copy Markdown

Braintrust eval report

Catalog Opportunity Analysis Evaluation (HEAD-1769524805)

ScoreAverageImprovementsRegressions
Catalog_availability21.6% (-2pp)-3 🔴
Llm_calls0 (+0)--
Tool_calls0 (+0)--
Errors0 (+0)--
Llm_errors0 (+0)--
Tool_errors0 (+0)--
Prompt_tokens0tok (+0tok)--
Prompt_cached_tokens0tok (+0tok)--
Prompt_cache_creation_tokens0tok (+0tok)--
Completion_tokens0tok (+0tok)--
Completion_reasoning_tokens0tok (+0tok)--
Total_tokens0tok (+0tok)--
Duration44.4s (+6.25s)2 🟢3 🔴

Catalog Songs Count Evaluation (HEAD-1769524805)

ScoreAverageImprovementsRegressions
AnswerCorrectness18.9% (-1pp)-3 🔴
Factuality33.3% (-67pp)-2 🔴
Llm_calls4 (+0)--
Tool_calls0 (+0)--
Errors0 (+0)--
Llm_errors0 (+0)--
Tool_errors0 (+0)--
Prompt_tokens0tok (+0tok)--
Prompt_cached_tokens0tok (+0tok)--
Prompt_cache_creation_tokens0tok (+0tok)--
Completion_tokens0tok (+0tok)--
Completion_reasoning_tokens0tok (+0tok)--
Completion_accepted_prediction_tokens0tok (+0tok)--
Completion_rejected_prediction_tokens0tok (+0tok)--
Completion_audio_tokens0tok (+0tok)--
Total_tokens0tok (+0tok)--
Duration13.93s (+4.16s)-3 🔴

First Week Album Sales Evaluation (HEAD-1769524805)

ScoreAverageImprovementsRegressions
Factuality60% (-20pp)-2 🔴
Llm_calls1 (+0)--
Tool_calls0 (+0)--
Errors0 (+0)--
Llm_errors0 (+0)--
Tool_errors0 (+0)--
Prompt_tokens0tok (+0tok)--
Prompt_cached_tokens0tok (+0tok)--
Prompt_cache_creation_tokens0tok (+0tok)--
Completion_tokens0tok (+0tok)--
Completion_reasoning_tokens0tok (+0tok)--
Completion_accepted_prediction_tokens0tok (+0tok)--
Completion_rejected_prediction_tokens0tok (+0tok)--
Completion_audio_tokens0tok (+0tok)--
Total_tokens0tok (+0tok)--
Duration14.27s (-2.89s)4 🟢-

Memory & Storage Tools Evaluation (HEAD-1769524805)

ScoreAverageImprovementsRegressions
Tools_called0% (+0pp)--
Llm_calls0 (+0)--
Tool_calls0 (+0)--
Errors0 (+0)--
Llm_errors0 (+0)--
Tool_errors0 (+0)--
Prompt_tokens0tok (+0tok)--
Prompt_cached_tokens0tok (+0tok)--
Prompt_cache_creation_tokens0tok (+0tok)--
Completion_tokens0tok (+0tok)--
Completion_reasoning_tokens0tok (+0tok)--
Total_tokens0tok (+0tok)--
Duration16.39s (+1.06s)-1 🔴

Monthly Listeners Tracking Evaluation (HEAD-1769524805)

ScoreAverageImprovementsRegressions
AnswerSimilarity80.2% (+2pp)3 🟢2 🔴
Llm_calls2 (+0)--
Tool_calls0 (+0)--
Errors0 (+0)--
Llm_errors0 (+0)--
Tool_errors0 (+0)--
Prompt_tokens0tok (+0tok)--
Prompt_cached_tokens0tok (+0tok)--
Prompt_cache_creation_tokens0tok (+0tok)--
Completion_tokens0tok (+0tok)--
Completion_reasoning_tokens0tok (+0tok)--
Total_tokens0tok (+0tok)--
Duration12.87s (-0.09s)3 🟢2 🔴

Search Web Tool Evaluation (HEAD-1769524805)

ScoreAverageImprovementsRegressions
AnswerCorrectness28.4% (+2pp)4 🟢6 🔴
Llm_calls3 (+0)--
Tool_calls0 (+0)--
Errors0 (+0)--
Llm_errors0 (+0)--
Tool_errors0 (+0)--
Prompt_tokens0tok (+0tok)--
Prompt_cached_tokens0tok (+0tok)--
Prompt_cache_creation_tokens0tok (+0tok)--
Completion_tokens0tok (+0tok)--
Completion_reasoning_tokens0tok (+0tok)--
Completion_accepted_prediction_tokens0tok (+0tok)--
Completion_rejected_prediction_tokens0tok (+0tok)--
Completion_audio_tokens0tok (+0tok)--
Total_tokens0tok (+0tok)--
Duration27.86s (+4.34s)3 🟢8 🔴

Social Scraping Evaluation (HEAD-1769524805)

ScoreAverageImprovementsRegressions
Tools_called0% (+0pp)--
Llm_calls0 (+0)--
Tool_calls0 (+0)--
Errors0 (+0)--
Llm_errors0 (+0)--
Tool_errors0 (+0)--
Prompt_tokens0tok (+0tok)--
Prompt_cached_tokens0tok (+0tok)--
Prompt_cache_creation_tokens0tok (+0tok)--
Completion_tokens0tok (+0tok)--
Completion_reasoning_tokens0tok (+0tok)--
Total_tokens0tok (+0tok)--
Duration26.56s (+4.01s)2 🟢4 🔴

Spotify Followers Evaluation (HEAD-1769524805)

ScoreAverageImprovementsRegressions
AnswerCorrectness20.7% (+0pp)1 🟢4 🔴
Llm_calls3 (+0)--
Tool_calls0 (+0)--
Errors0 (+0)--
Llm_errors0 (+0)--
Tool_errors0 (+0)--
Prompt_tokens0tok (+0tok)--
Prompt_cached_tokens0tok (+0tok)--
Prompt_cache_creation_tokens0tok (+0tok)--
Completion_tokens0tok (+0tok)--
Completion_reasoning_tokens0tok (+0tok)--
Completion_accepted_prediction_tokens0tok (+0tok)--
Completion_rejected_prediction_tokens0tok (+0tok)--
Completion_audio_tokens0tok (+0tok)--
Total_tokens0tok (+0tok)--
Duration13.17s (+1.21s)1 🟢4 🔴

Spotify Tools Evaluation (HEAD-1769524805)

ScoreAverageImprovementsRegressions
Tools_called0% (+0pp)--
Llm_calls0 (+0)--
Tool_calls0 (+0)--
Errors0 (+0)--
Llm_errors0 (+0)--
Tool_errors0 (+0)--
Prompt_tokens0tok (+0tok)--
Prompt_cached_tokens0tok (+0tok)--
Prompt_cache_creation_tokens0tok (+0tok)--
Completion_tokens0tok (+0tok)--
Completion_reasoning_tokens0tok (+0tok)--
Total_tokens0tok (+0tok)--
Duration34.28s (+9.45s)-2 🔴

TikTok Analytics Questions Evaluation (HEAD-1769524805)

ScoreAverageImprovementsRegressions
Question_answered10% (+10pp)1 🟢-
Llm_calls0 (+0)--
Tool_calls0 (+0)--
Errors0 (+0)--
Llm_errors0 (+0)--
Tool_errors0 (+0)--
Prompt_tokens0tok (+0tok)--
Prompt_cached_tokens0tok (+0tok)--
Prompt_cache_creation_tokens0tok (+0tok)--
Completion_tokens0tok (+0tok)--
Completion_reasoning_tokens0tok (+0tok)--
Total_tokens0tok (+0tok)--
Duration17.3s (-1.78s)1 🟢1 🔴

Copy link
Copy Markdown
Contributor

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.
@sidneyswift
sidneyswift merged commit 4823eab into testJan 27, 2026
5 checks passed
sidneyswift added a commit that referenced this pull request Jan 27, 2026
* fix: use upsert instead of insert for rooms
One-line fix: change insert to upsert in insertRoom.ts.
Removes try/catch workarounds since upsert handles duplicates.
sidneyswift added a commit that referenced this pull request Jan 27, 2026
sidneyswift added a commit that referenced this pull request Jan 27, 2026
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@sidneyswift@sweetmantech