Skip to content

fix(likes): dedupe parallel toggles to prevent count inflation and 500s#84

Draft
cursor[bot] wants to merge 1 commit into
mainfrom
cursor/critical-correctness-bugs-f3e5
Draft

fix(likes): dedupe parallel toggles to prevent count inflation and 500s#84
cursor[bot] wants to merge 1 commit into
mainfrom
cursor/critical-correctness-bugs-f3e5

Conversation

@cursor
Copy link
Copy Markdown

@cursor cursor Bot commented May 25, 2026

Bug and impact

Parallel POST /api/likes requests (e.g. double-click) could both observe no existing row and insert duplicates for the same url + ipHash. That:

  • Inflated the public like count (count was rows.length)
  • Made the next toggle call .unique() on by_url_ip, which throws when multiple rows exist → 500s and a broken like button for that visitor/page

Same class of race as the vote-count bug fixed in #71.

Root cause

likes.toggle used .unique() for read-modify-write without deduping duplicate rows created by overlapping mutations.

Fix

  • Collect by_url_ip rows and delete extras before/after toggle
  • Count distinct ipHash values per URL in aggregate so reads stay correct even if orphans exist

Validation

  • Added scripts/like-race-test.mjs (10 parallel toggles; fails if count jumps by >1)
  • Manual code review against the vote dedupe pattern in convex/lib/votes.js
Open in Web View Automation 

Concurrent like POSTs could insert duplicate rows for the same url+ipHash.
That inflated counts and made .unique() throw on the next toggle (500s).

Collect rows instead of unique(), delete duplicate rows after each toggle,
and count distinct ipHashes in aggregate so reads stay correct.

Co-authored-by: Injoon Oh <injoon5@icloud.com>
@vercel
Copy link
Copy Markdown

vercel Bot commented May 25, 2026

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

Project Deployment Actions Updated (UTC)
web Error Error May 25, 2026 3:08pm

@cloudflare-workers-and-pages
Copy link
Copy Markdown

Deploying web with  Cloudflare Pages  Cloudflare Pages

Latest commit: 4a4e7fc
Status:🚫  Build failed.

View logs

Sign up for free to 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.

1 participant