Skip to content

fix: keep the client working when the evaluation storage fails - #359

Open
duyhungtnn wants to merge 4 commits into
mainfrom
fix/keep-client-on-storage-failure
Open

duyhungtnn wants to merge 4 commits into
mainfrom
fix/keep-client-on-storage-failure

Conversation

@duyhungtnn

@duyhungtnn duyhungtnn commented Nov 13, 2026 •

Copy link
Copy Markdown
Collaborator

Note

Storage failures are rare, but when one happens at init, the client cannot recover for the whole session. This PR keeps the SDK working in that case.

Context

While working on #357 (evaluation sync status listener), a cache load failure at init needed its own sync status. This was a good time to decide how the client should handle this case. Both PRs touch this path in initializeBKTClientInternal; whichever merges second will be adjusted.

The new behavior matches the Android SDK: a failed cache load is ignored (code), and reads then return an empty list from memory (code).

Problem

When the evaluation storage fails at init, the client does not work at all.

If EvaluationStorage.initialize() cannot read the saved evaluations, the cache is never loaded and every later read throws Cache Evaluation entity is not loaded. initializeBKTClient() then rejects and the client is removed: getBKTClient() returns null, and a retry fails the same way. A failed save at init (in updateFeatureTag()) has the same result.

Examples:

  • Read fails: storage turned off in the browser; saved data changed by another script or in DevTools (JSON.parse() throws); the app's own storageFactory fails.
  • Save fails: localStorage is full (about 5 MB per site, shared with the app); the app's own storageFactory fails.

Solution

When the cache cannot be loaded, create default data in memory and keep going.

  • Read fails in initialize(): log the error and start with the default (empty) data, like a first launch.
  • Save fails in saveAsync(): log the error and continue. Memory is already updated, so update listeners are still called.

Same approach as the existing storage error handling in fetchEvaluationsInternal (code).

Result

  • The client stays registered. Flags return the app's default value until a fetch succeeds.
  • For storage errors, initializeBKTClient() rejects only because of the init fetch (for example a timeout), as the docs describe.
  • updateUserAttributes() no longer rejects when the save fails, and now reconnects streaming in that case.

Not in this PR

  • Event storage: events can still be lost and track() can still fail when storage fails. A following PR may keep events in memory.
  • The try/catch in initializeBKTClientInternal stays as a safety net for other failures (for example storage that throws when it is created).

Tests

  • EvaluationStorage: a failed read gives default data; a second initialize() still throws; a failed save still updates memory in all five save methods.
  • BKTClient: with failing storage, init resolves, the client stays registered, listeners are called, and values come from memory. A timed-out init fetch rejects with TimeoutException and variations return the default value. updateUserAttributes() resolves and reconnects.

At init, EvaluationStorage.initialize() loads the saved evaluations from
storage into memory. If this read failed, the cache was never loaded and
every later read threw, so initializeBKTClient() rejected and the client
was removed. A failed save at init (in updateFeatureTag()) had the same
result.

When the cache cannot be loaded, create default data in memory and keep
going:
- a failed read logs the error and starts with the default (empty) data,
  the same as a first launch
- a failed save logs the error and continues; memory is already updated

The client stays registered, flags return the app's default value until
a fetch succeeds, and init rejects only because of the init fetch.
@duyhungtnn
duyhungtnn force-pushed the fix/keep-client-on-storage-failure branch from ca885dc to aa55898 Compare November 13, 2026 03:40
Add tests for a failing storage write in update(),
setUserAttributesUpdated() and clearUserAttributesUpdated(), and check
that updateUserAttributes() now resolves and reconnects streaming in
that case.

Also fix the comment in initialize(): a full storage fails a save, not
a read.

Copilot AI 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.

🟢 Approval recommended

No active review issues are reported for the change that preserves SDK usability when evaluation storage fails.

No open issues 🎉

What changed in this PR

This PR keeps the SDK usable when evaluation storage reads or writes fail by falling back to an in-memory cache instead of failing initialization.

Changes:

  • Initialize an empty cache after storage read errors and preserve in-memory updates after save errors.
  • Add regression tests for initialization, evaluation listeners, default values, and streaming reconnection when storage fails.
  • Clarify initialization comments to distinguish recoverable storage errors from fatal failures.
File Description
test/​internal/​evaluation/​EvaluationStorage.spec.ts Tests in-memory fallback and updates after storage failures.
test/​BKTClient.spec.ts Tests client availability, fetch outcomes, listeners, and reconnection with failing storage.
src/​internal/​evaluation/​EvaluationStorage.ts Logs storage errors, retains usable memory state, and shares default entity creation.
src/​BKTClient.ts Updates comments describing initialization error handling; no runtime changes.

Review effort: Balanced

@duyhungtnn
duyhungtnn requested a review from cre8ivejp November 13, 2026 07:21
@duyhungtnn
duyhungtnn marked this pull request as ready for review November 13, 2026 07:21
@duyhungtnn

Copy link
Copy Markdown
Collaborator Author

hi @cre8ivejp please help me to take a look

Copilot AI balanced review requested due to automatic review settings November 13, 2026 08:07

Copilot AI 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.

🟢 Approval recommended

No active review issues remain for the change that preserves SDK operation when evaluation storage fails.

No open issues 🎉

Review effort: Balanced

@duyhungtnn
duyhungtnn marked this pull request as draft November 17, 2026 06:45
@duyhungtnn
duyhungtnn marked this pull request as ready for review November 19, 2026 09:03
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