Skip to content

temp fix to race condition#70

Merged
jamesholcombe merged 2 commits intomainfrom
fix/race-condition
Aug 7, 2025
Merged

temp fix to race condition#70
jamesholcombe merged 2 commits intomainfrom
fix/race-condition

Conversation

@jamesholcombe
Copy link
Copy Markdown
Member

No description provided.

@vercel
Copy link
Copy Markdown

vercel Bot commented Aug 7, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
groundup ✅ Ready (Inspect) Visit Preview 💬 Add feedback Aug 7, 2025 11:11am
groundup-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Aug 7, 2025 11:11am

This comment was marked as outdated.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR implements a temporary fix for a race condition when creating new users by adding a retry mechanism with exponential backoff. The changes address scenarios where multiple concurrent requests attempt to create the same user simultaneously.

  • Adds retry logic with exponential backoff to handle duplicate user creation attempts
  • Updates the createUser function to use authId as the primary conflict resolution target instead of email
  • Reorders query conditions to check email first, then authId

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
packages/app/src/lib/auth.ts Implements retry mechanism with exponential backoff for user creation
packages/app/src/db/crud/user.ts Updates conflict resolution to use authId as target and reorders query conditions

termsOfUseAcceptedAt: new Date(),
});
break; // Success, exit the retry loop
} catch (error: any) {
Copy link

Copilot AI Aug 7, 2025

Choose a reason for hiding this comment

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

Using 'any' type for error handling defeats TypeScript's type safety. Consider using 'unknown' or a more specific error type to maintain type safety.

Suggested change
} catch (error: any) {
} catch (error: unknown) {

Copilot uses AI. Check for mistakes.
retryCount++;

// If it's a duplicate key error and we haven't exceeded max retries, try again
if (retryCount < maxRetries) {
Copy link

Copilot AI Aug 7, 2025

Choose a reason for hiding this comment

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

The retry logic doesn't check for specific error types. This could cause unnecessary retries for errors that aren't related to race conditions (e.g., network errors, validation errors). Consider checking for specific database constraint violation errors before retrying.

Copilot uses AI. Check for mistakes.
// this is done in transaction to avoid race condition when creating user, for conflicts on authId
return await db.transaction(async (trx) => {
// First try to find the user by authId
// Also check by email to handle edge cases
Copy link

Copilot AI Aug 7, 2025

Choose a reason for hiding this comment

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

The comment 'Also check by email to handle edge cases' is vague. It should explain what specific edge cases this addresses and why checking both email and authId is necessary.

Suggested change
// Also check by email to handle edge cases
// Check for an existing user with both the same email and authId.
// This handles edge cases where:
// - A user may have registered with the same email address using different authentication providers (resulting in different authIds).
// - There may be attempts to create duplicate accounts with the same email but different authIds, or vice versa.
// By checking both fields, we ensure that we do not create duplicate user records for the same logical user across different auth providers.

Copilot uses AI. Check for mistakes.
target: [user.authId],
set: {
authId: userInsert.authId,
email: userInsert.email,
Copy link

Copilot AI Aug 7, 2025

Choose a reason for hiding this comment

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

Changing the conflict target from email to authId could have unintended consequences. If the same email is used with different authIds, this will now allow duplicate emails instead of updating the existing user's authId. This seems inconsistent with the original logic.

Copilot uses AI. Check for mistakes.
@jamesholcombe jamesholcombe merged commit 0f308c6 into main Aug 7, 2025
3 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.

2 participants