Skip to content

fix(sdk/react-native): prevent duplicate requests by flagging fetch-t… - #18

Merged
buivietphi merged 2 commits into
mainfrom
fix/rn-sdk-dup-requests
Jul 13, 2026
Merged

buivietphi merged 2 commits into
mainfrom
fix/rn-sdk-dup-requests

Conversation

@buivietphi

@buivietphi buivietphi commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

…riggered XHR calls

Description

Related Issue

Type of Change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation update
  • Performance improvement
  • CI/CD or build configuration
  • Other (describe below)

Testing

  • Tested on macOS
  • Tested on Windows
  • Flutter analyze passes
  • SDK build passes (if applicable)

Screenshots (if applicable)

Summary by CodeRabbit

  • Bug Fixes
    • Prevented duplicate network activity reporting when fetch requests run over an underlying XHR transport.
    • Improved request tracking so fetch and XHR events are correctly associated as part of the same operation.

@coderabbitai

coderabbitai Bot commented Jul 13, 2026

Copy link
Copy Markdown

Review Change Stack

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 5aaaf0d9-8fab-462c-9f70-7d06550c8c39

📥 Commits

Reviewing files that changed from the base of the PR and between 753d23b and 15cbfa2.

📒 Files selected for processing (1)
  • client_sdks/devconnect-react-native/src/client.ts

📝 Walkthrough

Walkthrough

The React Native client replaces a boolean fetch-state flag with a shared counter, handles fetch success and failure cleanup, and uses the captured state with existing request-key tracking to suppress duplicate XHR reporting.

Changes

React Native network deduplication

Layer / File(s) Summary
Track fetch execution state
client_sdks/devconnect-react-native/src/client.ts
Adds fetchStackCount, updates it around originalFetch, and preserves completion and in-flight bookkeeping on rejected fetches.
Apply fetch state to XHR reporting
client_sdks/devconnect-react-native/src/client.ts
Combines captured fetch-stack state with fetchInFlight tracking to deduplicate XHR send and loadend reporting.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant FetchInterceptor
  participant DevConnect
  participant originalFetch
  participant XHRInterceptor
  FetchInterceptor->>DevConnect: increment fetchStackCount
  FetchInterceptor->>originalFetch: invoke originalFetch
  originalFetch-->>FetchInterceptor: resolve or reject
  XHRInterceptor->>DevConnect: read fetchStackCount
  DevConnect-->>XHRInterceptor: provide isFetchXhr
  XHRInterceptor->>XHRInterceptor: suppress duplicate send/loadend reporting
  FetchInterceptor->>DevConnect: decrement fetchStackCount in finally
Loading

Poem

Fetches count, then safely unwind,
XHR echoes leave no trace behind.
Success or failure, states align,
One network story crosses the line.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the PR’s main change: deduplicating React Native requests by detecting fetch-triggered XHR traffic.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/rn-sdk-dup-requests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a boolean flag isInsideFetch to track when the SDK is executing an internal fetch call, allowing the PatchedXHR implementation to skip duplicate reporting of requests initiated by fetch. The reviewer identified two main issues: re-entrancy issues where nested fetch calls could prematurely reset the boolean flag (suggesting a counter fetchStackCount instead), and a synchronous error handling regression where synchronous exceptions thrown by originalFetch would bypass the main try-catch block and prevent completion events from being sent.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

* own duplicate report if the count is > 0.
*/
private fetchInFlight: Map<string, number> = new Map();
private isInsideFetch = false;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

Using a simple boolean flag isInsideFetch can lead to re-entrancy issues if there are nested fetch calls (e.g., inside custom fetch wrappers or interceptors). When a nested fetch completes, it will set isInsideFetch to false, which prematurely clears the flag for the outer fetch call that is still in progress.

Consider using a counter (e.g., fetchStackCount) instead of a boolean flag to safely track nested/concurrent fetch invocations.

Suggested change
private isInsideFetch = false;
private fetchStackCount = 0;

Comment on lines +924 to +930
let responsePromise: Promise<Response>;
dc.isInsideFetch = true;
try {
const response = await originalFetch(input, init);
responsePromise = originalFetch(input, init);
} finally {
dc.isInsideFetch = false;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

There are two issues with this implementation:

  1. Synchronous Error Handling Regression: If originalFetch throws an error synchronously (e.g., due to invalid arguments or synchronous validation in another interceptor), the error will propagate immediately and bypass the main catch block starting at line 953. This means the client:network:request_complete event will never be sent, leaving the request permanently in a pending state in the UI.
  2. Re-entrancy / Nested Fetches: If a nested fetch is triggered, the finally block of the nested fetch will set isInsideFetch to false prematurely, clearing the flag for the outer fetch.

We can resolve both issues by:

  • Wrapping the synchronous call to originalFetch so that any synchronous exceptions are captured as a rejected promise.
  • Using a counter (fetchStackCount) instead of a boolean flag.
      let responsePromise: Promise<Response>;
      dc.fetchStackCount++;
      try {
        responsePromise = Promise.resolve(originalFetch(input, init));
      } catch (err) {
        responsePromise = Promise.reject(err);
      } finally {
        dc.fetchStackCount--;
      }


function PatchedXHR(this: any) {
const xhr = new OriginalXHR();
const isFetchXhr = dc.isInsideFetch;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

Update this to check the counter fetchStackCount > 0 instead of the boolean isInsideFetch.

Suggested change
const isFetchXhr = dc.isInsideFetch;
const isFetchXhr = dc.fetchStackCount > 0;

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@client_sdks/devconnect-react-native/src/client.ts`:
- Around line 924-933: Update the fetch wrapper around originalFetch and
responsePromise so synchronous exceptions are handled by the existing request
error/finalization flow. Nest the isInsideFetch toggle and originalFetch call
within the try that catches request failures, while retaining the finally reset,
ensuring request_complete error telemetry is emitted and fetchInFlight is
decremented for synchronous throws.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: faf5088c-1838-4cbe-aad1-32cb7d83f84e

📥 Commits

Reviewing files that changed from the base of the PR and between 5128871 and 753d23b.

📒 Files selected for processing (1)
  • client_sdks/devconnect-react-native/src/client.ts

Comment thread client_sdks/devconnect-react-native/src/client.ts
@buivietphi
buivietphi merged commit 149be6f into main Jul 13, 2026
4 of 5 checks passed
buivietphi pushed a commit that referenced this pull request Jul 13, 2026
Publishes the fetchStackCount counter refactor from PR #18 to npm.
The 1.0.7 release shipped before that refactor was merged, so the
counter-based dedup was not yet on the registry.

Co-Authored-By: Claude <noreply@anthropic.com>
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