Skip to content

fix(habits): handle concurrent toggle race in habit log create#68

Merged
TemaDeveloper merged 1 commit into
TemaDeveloper:mainfrom
Chzhqv:fix/habit-log-toggle-race
Jul 7, 2026
Merged

fix(habits): handle concurrent toggle race in habit log create#68
TemaDeveloper merged 1 commit into
TemaDeveloper:mainfrom
Chzhqv:fix/habit-log-toggle-race

Conversation

@Chzhqv

@Chzhqv Chzhqv commented Jul 7, 2026

Copy link
Copy Markdown

Summary

  • Two concurrent toggle-on requests for the same habit+day could both observe "no existing log" and both call HabitLog.create(), tripping the unique habitId+date index and crashing with an unhandled 500 on the losing request.
  • Catch the duplicate-key error (code 11000) and report toggled: true instead, since a log exists either way once the race resolves.

Test plan

  • Fired two truly simultaneous requests server-side (curl, dispatched with &/wait) against a running dev server + real MongoDB — got 201 + 200, no 500, confirmed exactly one HabitLog document afterward (no duplicate)
  • Repeated the race live from the browser console against a real logged-in session — no crash on either interleaving observed
  • Confirmed normal single toggle-on/toggle-off still works
  • pnpm lint, tsc --noEmit, full vitest run (556/556) all pass

Fixes #1

Two concurrent toggle-on requests for the same habit+day could both
observe "no existing log" and both call HabitLog.create(), tripping
the unique habitId+date index and crashing with an unhandled 500 on
the losing request. Catch the duplicate-key error and report
toggled:true instead, since a log exists either way.

Fixes TemaDeveloper#1
@vercel

vercel Bot commented Jul 7, 2026

Copy link
Copy Markdown

Someone is attempting to deploy a commit to the Artemii's projects Team on Vercel.

A member of the Team first needs to authorize it.

@TemaDeveloper TemaDeveloper left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@TemaDeveloper TemaDeveloper merged commit 9fa29ae into TemaDeveloper:main Jul 7, 2026
1 check failed
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.

bug: Race condition in habit log toggle

2 participants