Skip to content

fix(social): Make CreateFriendship idempotent#20

Merged
x64-dev merged 1 commit into
mainfrom
seer/fix/social-friendship-idempotency
Jun 5, 2026
Merged

fix(social): Make CreateFriendship idempotent#20
x64-dev merged 1 commit into
mainfrom
seer/fix/social-friendship-idempotency

Conversation

@seer-by-sentry

@seer-by-sentry seer-by-sentry Bot commented Jun 5, 2026

Copy link
Copy Markdown
Contributor

This PR addresses the Microsoft.EntityFrameworkCore.DbUpdateException occurring when CreateFriendship is called multiple times for the same user pair, leading to a duplicate primary key violation in the Friends table.

The root cause was that CreateFriendship unconditionally attempted to insert a new FriendEntry without first checking for its existence. This could happen due to race conditions or repeated client requests.

The solution implements a two-layer defense:

  1. An AnyAsync() check is added before insertion to prevent adding duplicate entries in most cases.
  2. The catch block is enhanced to specifically handle MySqlConnector.MySqlException with error code 1062 (ER_DUP_ENTRY). This ensures that if a race condition still leads to a duplicate insert attempt, the exception is silently ignored, as the desired state (friendship exists) has already been achieved by a concurrent operation.

This makes the CreateFriendship method idempotent and prevents the DbUpdateException.

Fixes SERVICES-3W

@x64-dev x64-dev merged commit d2ccad8 into main Jun 5, 2026
16 checks passed
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