Skip to content

Conversation

@zdql
Copy link
Contributor

@zdql zdql commented Nov 25, 2025

No description provided.

@vercel
Copy link
Contributor

vercel bot commented Nov 25, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
assistant-ui-template Ready Ready Preview Comment Nov 25, 2025 11:30pm
echo-control Ready Ready Preview Comment Nov 25, 2025 11:30pm
echo-next-boilerplate Ready Ready Preview Comment Nov 25, 2025 11:30pm
echo-next-image Ready Ready Preview Comment Nov 25, 2025 11:30pm
echo-next-sdk-example Ready Ready Preview Comment Nov 25, 2025 11:30pm
echo-video-template Ready Ready Preview Comment Nov 25, 2025 11:30pm
echo-vite-sdk-example Ready Ready Preview Comment Nov 25, 2025 11:30pm
next-chat-template Ready Ready Preview Comment Nov 25, 2025 11:30pm
react-boilerplate Ready Ready Preview Comment Nov 25, 2025 11:30pm
react-chat Ready Ready Preview Comment Nov 25, 2025 11:30pm
react-image Ready Ready Preview Comment Nov 25, 2025 11:30pm
1 Skipped Deployment
Project Deployment Preview Comments Updated (UTC)
component-registry Skipped Skipped Nov 25, 2025 11:30pm

@railway-app
Copy link

railway-app bot commented Nov 25, 2025

🚅 Deployed to the echo-pr-698 environment in echo

Service Status Web Updated (UTC)
echo ◻️ Removed (View Logs) Nov 26, 2025 at 12:05 am

Comment on lines 35 to 57
const membership = await utils.apps.app.memberships.get
.fetch({ appId })
.catch(() => null);

// Only update if user has no referrer yet
if (membership?.referrerId === null) {
await updateReferrer({
appId,
referrerId: referralCodeData.id,
}).catch(() => {
// Silently fail - user might already have a referrer from a race condition
});
}

// Create membership if it doesn't exist
if (!membership) {
await createMembership({
appId,
referrerId: referralCodeData.id,
}).catch(() => {
// Silently fail - membership might have been created in the meantime
});
}
Copy link
Contributor

@vercel vercel bot Nov 25, 2025

Choose a reason for hiding this comment

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

The component conflates API fetch errors with "membership doesn't exist", potentially causing duplicate membership creation attempts if the membership fetch fails.

View Details
📝 Patch Details
diff --git a/packages/app/control/src/app/(app)/app/[id]/(overview)/_components/referral-handler.tsx b/packages/app/control/src/app/(app)/app/[id]/(overview)/_components/referral-handler.tsx
index ed0746c0..0e4d2ae9 100644
--- a/packages/app/control/src/app/(app)/app/[id]/(overview)/_components/referral-handler.tsx
+++ b/packages/app/control/src/app/(app)/app/[id]/(overview)/_components/referral-handler.tsx
@@ -23,18 +23,32 @@ export const ReferralHandler: React.FC<Props> = ({ appId }) => {
     if (!referralCode || processed) return;
 
     const processReferralCode = async () => {
-      const referralCodeData = await utils.apps.app.referralCode.get.byCode
-        .fetch(referralCode)
-        .catch(() => null);
+      let referralCodeData;
+      try {
+        referralCodeData = await utils.apps.app.referralCode.get.byCode.fetch(
+          referralCode
+        );
+      } catch (error) {
+        console.error('Failed to fetch referral code:', error);
+        setProcessed(true);
+        return;
+      }
 
       if (!referralCodeData) {
         setProcessed(true);
         return;
       }
 
-      const membership = await utils.apps.app.memberships.get
-        .fetch({ appId })
-        .catch(() => null);
+      let membership;
+      try {
+        membership = await utils.apps.app.memberships.get.fetch({ appId });
+      } catch (error) {
+        // Failed to fetch membership - cannot distinguish between "doesn't exist" and "fetch failed"
+        // Avoid attempting to create membership when we don't know the actual state
+        console.error('Failed to fetch membership:', error);
+        setProcessed(true);
+        return;
+      }
 
       // Only update if user has no referrer yet
       if (membership?.referrerId === null) {
@@ -46,7 +60,7 @@ export const ReferralHandler: React.FC<Props> = ({ appId }) => {
         });
       }
 
-      // Create membership if it doesn't exist
+      // Create membership if it doesn't exist (and fetch succeeded confirming non-existence)
       if (!membership) {
         await createMembership({
           appId,

Analysis

ReferralHandler conflates API fetch errors with membership non-existence, risking duplicate membership creation

What fails: In ReferralHandler.processReferralCode(), when memberships.get.fetch() fails with a network or server error (lines 35-37), it's silently caught and converted to null via .catch(() => null). This creates ambiguity: the code cannot distinguish between "membership doesn't exist" (legitimate case where create is safe) and "membership fetch failed" (error case where state is unknown). Result: if the fetch fails when a membership already exists, the code proceeds to attempt creating a duplicate membership at line 50.

How to reproduce:

  1. User with an existing membership visits the app with a referral code query parameter
  2. Network failure or server error occurs during the memberships.get.fetch({ appId }) call
  3. The .catch(() => null) suppresses the error and sets membership to null
  4. Code reaches line 50 where if (!membership) evaluates to true
  5. Code attempts createMembership() even though the membership likely exists

Result: Multiple membership creation attempts triggered by transient API errors, leading to:

  • Unexpected API responses from duplicate creation attempts (membership already exists)
  • Potential for data inconsistency if error handling in create/update operations relies on specific error codes
  • User confusion or silent failures

Expected behavior: Errors during membership fetch should be handled distinctly from the legitimate "no membership exists" case. If we cannot determine membership state due to a fetch error, we should not attempt creation.

Fix implemented: Replaced .catch(() => null) pattern with explicit try-catch blocks that:

  • Distinguish fetch errors from successful null responses
  • Log errors for observability
  • Return early when fetch fails (rather than attempting creation with unknown state)
  • Only proceed to create membership when fetch succeeds and returns null (confirming non-existence)

This ensures duplicate creation attempts only occur due to documented race conditions (lines 54, 65 comments), not API failures.

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