Skip to content

Comments

fix(UpdatePlayerGameMetricsAction): use firstOrCreate to prevent race condition duplicate inserts#4587

Open
wescopeland wants to merge 2 commits intoRetroAchievements:masterfrom
wescopeland:player-game-metrics-race-cond-fix
Open

fix(UpdatePlayerGameMetricsAction): use firstOrCreate to prevent race condition duplicate inserts#4587
wescopeland wants to merge 2 commits intoRetroAchievements:masterfrom
wescopeland:player-game-metrics-race-cond-fix

Conversation

@wescopeland
Copy link
Member

Resolves:
Screenshot 2026-02-22 at 11 44 09 AM

Root Cause
Two concurrent UpdatePlayerGameMetricsJob instances for the same user+game can both query for an existing PlayerAchievementSet. Both find nothing, both try to INSERT, and the second one hits a unique constraint.

We're basically hand-rolling a first() + new + save() pattern. This PR replaces that pattern with a firstOrCreate() which should handle the operation atomically.

@wescopeland wescopeland requested a review from a team February 22, 2026 16:48
continue;
if (!$hasExistingMetrics) {
continue;
}
Copy link
Member

Choose a reason for hiding this comment

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

Suggest just fetching this here to avoid an extra lookup in a few lines:

if ($isUntouchedSubset) {
    $playerAchievementSet = PlayerAchievementSet...first();
    if (!$playerAcheivementSet) {
        continue;
    }
} else {
    $playerAchievementSet = PlayerAchievementSet::firstOrCreate(...);
}

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.

2 participants