Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions GenOnlineService/Database/Database.Social.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,14 @@ public static async Task CreateFriendship(AppDbContext db, long userId1, long us
{
try
{
// Check if friendship already exists (handles duplicate calls / race conditions)
bool alreadyExists = await db.Friends.AnyAsync(f =>
(f.UserId1 == userId1 && f.UserId2 == userId2) ||
(f.UserId1 == userId2 && f.UserId2 == userId1));

if (alreadyExists)
return;

db.Friends.Add(new FriendEntry
{
UserId1 = userId1,
Expand All @@ -208,6 +216,12 @@ public static async Task CreateFriendship(AppDbContext db, long userId1, long us
}
catch (Exception ex)
{
// If two concurrent calls both passed the existence check, MySQL will throw a
// duplicate-key error (ER_DUP_ENTRY, code 1062). The friendship was already
// created by the other call, so this is not an error worth reporting.
if (ex.InnerException is MySqlConnector.MySqlException mysqlEx && mysqlEx.Number == 1062)
return;

Console.WriteLine($"[ERROR] CreateFriendship failed: {ex.Message}");
SentrySdk.CaptureException(ex);
}
Expand Down
Loading