Skip to content

fix: [SDK-5373] send session requests once and classify by status (1/3) - #1576

Open
fadi-george wants to merge 3 commits into
new-session-eventsfrom
fadi/sdk-5373-sessions-single-attempt
Open

fadi-george wants to merge 3 commits into
new-session-eventsfrom
fadi/sdk-5373-sessions-single-attempt

Conversation

@fadi-george

@fadi-george fadi-george commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Follow-up to #1569 (SDK-5373), into new-session-events.

Description

1 Line Summary

Send session requests once with no built-in retry, and classify the result by status even when the body isn't JSON, matching Android.

Details

Motivation

Review of #1569 found that the sessions client could misclassify some responses and would retry inside base.ts before the queue saw a result. I reproduced each case against the PR head:

  • A network failure went through executeFetch's internal retries (5 attempts, 10/20/30/30/30s delays, ~2 min) before returning retry. The queue owns backoff, and a service worker can be killed during those waits.
  • executeFetch parses JSON before returning the status. Any non-JSON response became retry, so an HTML 400/404 from a CDN or WAF would retry forever. A 204 or a 202 with an empty body on update would also retry forever.
  • An invalid startTime/endTime threw a RangeError instead of returning a result.

Android (OneSignal/OneSignal-Android-SDK#2775) avoids all three: its HttpClient makes one attempt, returns status 0 on a network failure, and checks the status before parsing the payload.

Scope

  • base.ts: new exported callOnce(method, path, data). It makes one fetch and returns result: undefined when the body is empty or not JSON (response.json().catch()), so status and Retry-After always come back. A Retry-After that is negative or not a number is dropped. Request building is split out into buildRequest and shared with call. The dead method || 'NO_METHOD_SPECIFIED' fallback is removed. Existing get/post/patch/put/delete behave the same.
  • sessions.ts: uses callOnce. Each body is built inside the request thunk, so an invalid time throws a RangeError there and send drops it without sending. A create 202 without a session id now passes Retry-After through.
  • callOnce is a separate export, not a flag on call(), so it is tree-shaken out of the page bundle. The base branch was already at the page.es6 size limit. Sizes (gzip -9 bytes): page.es6 46825 → 46820, sw 13649 → 13643. With the sessions client wired into the SW to measure its future cost, the SW is +71 B over the base branch's client, and most of that is the single-attempt path.
  • Other base.ts callers still retry internally and still throw on non-JSON. Moving them over would change the user and subscription flows, so it is out of scope here.

Systems Affected

  • WebSDK
  • Backend
  • Dashboard

Validation

Tests

Info

sessions.test.ts no longer mocks delay, so a hidden retry would time out. New cases: one attempt on network failure, non-JSON 400/403/404 drop, non-JSON 502 retries with status and Retry-After, empty 204/202 on update succeeds, empty 202 on create retries, Retry-After on a 202 without a session id, negative Retry-After ignored, and NaN/Infinity/out-of-range times drop without sending. vp check, full vp test (1076), and build:prod (size limits) pass.

Not tested in a browser; the sessions client has no callers yet.

Checklist

  • All the automated tests pass or I explained why that is not possible
  • I have personally tested this on my machine or explained why that is not possible
  • I have included test coverage for these changes or explained why they are not needed

Programming Checklist
Interfaces:

  • Don't use default export
  • New interfaces are in model files

Functions:

  • Don't use default export
  • All function signatures have return types
  • Helpers should not access any data but rather be given the data to operate on.

Typescript:

  • No Typescript warnings
  • Avoid silencing null/undefined warnings with the exclamation point

Other:

  • Iteration: refrain from using elem of array syntax. Prefer forEach or use map
  • Avoid using global OneSignal accessor for context if possible. Instead, we can pass it to function/constructor so that we don't call OneSignal.context

Screenshots

Checklist

  • I have included screenshots/recordings of the intended results or explained why they are not needed (no UI)

Related Tickets


🤖 Generated with Claude Code

@fadi-george fadi-george changed the title fix: [SDK-5373] send session requests once and classify by status fix: [SDK-5373] send session requests once and classify by status (1/2) Nov 13, 2026
@fadi-george
fadi-george added this pull request to stack #1578 November 13, 2026 01:29
@fadi-george fadi-george changed the title fix: [SDK-5373] send session requests once and classify by status (1/2) fix: [SDK-5373] send session requests once and classify by status (1/3) Nov 13, 2026
Comment thread src/shared/api/base.ts
if (!requestHasAppId(path, data)) throw AppIDMissingError;

const response = await fetch(...buildRequest(method, path, data, undefined));
const retryAfter = parseInt(response.headers.get('Retry-After') ?? '');

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: RangeError drop has no log, so a bad timestamp loses data with no message.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good catch, moved the log above the drop check so both cases log (d2c205b). Comment landed on base.ts but I think you meant sessions.ts:96.

@abdulraqeeb33 abdulraqeeb33 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Baseline is the unmerged parent branch new-session-events (no open parent PR).

callOnce sends each session request once. Status and Retry-After still come back when the body is empty or not JSON, and an invalid time is dropped before fetch.

fadi-george and others added 3 commits November 13, 2026 17:00
Session requests now go through base.callOnce: one attempt with no
built-in retry or backoff, and the status and Retry-After are returned
even when the body is empty or not JSON. A non-JSON 4xx now drops
instead of retrying forever, and an empty 2xx on update succeeds.
Invalid start or end times drop instead of throwing, and a negative
Retry-After is ignored.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
callOnce reads the body with response.json().catch() and validates
Retry-After itself. sessions builds each body inside the request thunk,
so an invalid time surfaces as a RangeError that send drops, replacing
the separate validation and Retry-After helpers.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@fadi-george
fadi-george force-pushed the fadi/sdk-5373-sessions-single-attempt branch from d2c205b to 4e9f0e6 Compare November 14, 2026 01:00
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.

3 participants