← Back to Pull requests

fix(listener): reject a modal request superseded by a newer one #520

Closed
unknown closed this pull request without merging on Nov 24, 2026 12:12 UTC
View on GitHub
unknown unknown commented on Nov 24, 2026 12:08 UTC
Author

When a merchant calls displayModal while an earlier modal request is still pending (a double click, or two widgets on one page), the earlier call never resolves or rejects. In @frak-labs/react-sdk, useDisplayModal and useSendTransaction then stay isPending for the rest of the page view, and an await on the first call hangs.

Summary

The handler unsubscribed the earlier request from the modal store but never settled its Deferred. The modal on screen emits to the newest request only, so nothing could settle the first promise.

Now the newest request owns the UI, and the request it replaces is rejected with -32603 "New modal request superseded previous request". That is the code and message shape the sharing-page handler already uses.

 handleDisplayModal(params)
-  activeUnsubscribe?.()                      # earlier deferred dropped, never settled
+  latestRequest = request
   if no steps
+    settleDisplayed?.()                      # reject the modal on screen
     clearModal(); throw invalidRequest
   await filterStepsToDo()                    # may refresh the SDK session (≤5 s)
+  if latestRequest !== request
+    throw superseded()                       # a newer request arrived meanwhile
+  settleDisplayed?.()                        # reject the request whose modal we replace
   setNewModal(); subscribe(); setRequest()
-  activeUnsubscribe = unsubscribe
+  settleDisplayed = () => { unsubscribe(); deferred.reject(superseded()) }

  • The modal on screen stays live until it is actually replaced. While a newer request waits on its session refresh, the user can still finish the modal on screen, and its caller gets the real result. Rejecting it the moment the newer request arrives would report -32603 for an action that happened. On a sendTransaction step, a merchant that retries on error could then send twice.

  • The error code stays internalError. clientAborted means "the user dismissed the modal" to SDK callers.
  • Evidence

    Unit tests (handleDisplayModal.test.ts). The first commit adds them and fails on the current code. The second commit fixes it.

    first commit, current impl                         Tests  4 failed | 1 passed (5)
      × rejects the pending request when a new one replaces it               (timeout: first never settles)
      × rejects the displayed request when a newer one has no steps          (timeout)
      × rejects a request replaced during the session refresh (×2 orders)    (setRequest called 2 times, expected 1)
      ✓ keeps the displayed modal live while a newer request refreshes       (current behavior, guarded)
    second commit                                      listener: Test Files 29 passed, Tests 227 passed
    

    Deleting each guard in the fix makes a test fail: the takeover settle, the latestRequest check, and the settle on the no-steps path.

    Browser. A Playwright script in headless Chromium loads a merchant page with the real SDK CDN bundle and the built listener iframe, against a mocked backend and seeded session (no SST stack). Steps: { login: {}, final: { action: { key: "reward" } } }.

    | Case | dev | This branch |
    |---|---|---|
    | 2nd call 100 ms after the 1st | 1st pending forever | 1st rejects -32603, 2nd modal shown, resolves on dismiss |
    | 2nd call during a session refresh delayed 800 ms (gaps 100/400 ms, 3 runs each) | 1st pending forever | 1st rejects -32603, only the 2nd's modal is ever shown, 2nd resolves on dismiss |
    | User completes the 1st modal while the 2nd waits on a 1.5 s refresh (3 runs) | 1st resolves with its result | 1st resolves with its result, then the 2nd modal is shown and resolves on dismiss |

    On a cold iframe with two calls in the same instant, the listener sometimes handles the second message first: both wait on the same lazy import() of the modal module. One call is rejected and the other settles in every run; which one wins can vary.

    Typecheck, biome and lint:comments are clean.

    Merge Danger

    Door: two-way. Listener only; reverting restores the old behavior.

    Blast Radius: SDK modal callers

    A merchant whose second displayModal replaces a pending one now gets a rejection on the first call where it used to hang. Code that awaits the first call and has no error handler will see an unhandled rejection instead of a silent hang.

    Not fixed here:

  • FRA-427: a login-only modal for a logged-in user crashes in trackModalDisplay. When it does, the next request's takeover rejects a promise nobody awaits, which logs "Uncaught (in promise)".

  • A sharing-page request that replaces a modal, or the reverse, likely leaves the other promise hanging. That is read from the code and not reproduced.
  • Diff: 595 lines (+446 / -149) across 7 files Reviewer: github-actions[bot] Review duration: 3m
    All Vouch review confidence checks passed
    Residual risk 0.34 = Change Risk (0.50) × (1 − Review Confidence (0.32)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.50 · 595 lines diff across 7 files
    0.50
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 20% · Reviewer familiarity: 35%
    31%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.382
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #520"
    general weight: 0.35
    Assignees
    Unassigned
    Labels
    risk: low confidence: 31.9% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 50% (MEDIUM) · Higher = larger blast radius
    50%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 31.9% · Lower = rushed or shallow review
    31.9%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 34.1% · Safe to merge
    34.1%
    Repository