Skip to content

feat: bounded background retry of failed feature fetches, async refresh_features - #174

Open
vazarkevych wants to merge 5 commits into
feat/request-timeoutfrom
feat/fetch-retry
Open

vazarkevych wants to merge 5 commits into
feat/request-timeoutfrom
feat/fetch-retry

Conversation

@vazarkevych

@vazarkevych vazarkevych commented Dec 3, 2026 •

Copy link
Copy Markdown
Collaborator

Stacked on #169 (request timeouts). The base branch is feat/request-timeout, so the diff shows only this PR's changes. Merge #169 first, then retarget this PR to main.

What and why

A failed feature fetch was never retried. The only retries were urllib3's immediate ones, which the request-timeout change disables, so a 5xx or a dropped connection left the SDK on cache or defaults until the next refresh, and the async initialize() made a single attempt.

What changed

  • Background retry: after a failed fetch the call returns at once with the best available features (cache, inline payload or defaults), and the fetch is retried in the background after 1, 2, 4, 8 and 16s. Each retry is logged (Feature fetch failed. Retry attempt n/N in Xms.), and after the last one the SDK stops. Retries never extend startup.
  • Only transient failures: connection errors, timeouts, 429 and 5xx. A 401/403/404, a response that isn't a valid payload, or one that fails to decrypt is reported once and not retried; hitting one during retries stops the loop.
  • Sync:
    • One retry loop per (api_host, client_key) in the shared feature_repo, not per instance, so an outage doesn't multiply requests by the number of instances. The first failing instance's settings apply, and a finished loop is dropped so keys don't accumulate threads.
    • Success reaches every instance on that key and no others: feature-update callbacks are now routed by (api_host, client_key), the same change as in feat: configurable polling interval, per-key pollers, refresh backoff #173. Callbacks registered without a key still receive every update.
    • The loop holds no instance, and the loop stops early once every instance on the key is destroyed.
    • A destroyed instance never receives the result, even one already being delivered when destroy() runs.
    • The lazy per-evaluation refresh doesn't start retries, since the next evaluation fetches again anyway.
  • Async: a failed initialize() still returns False. A successful retry installs the features and starts refreshing. Any successful initialization (a retry, a later initialize() or refresh_features()) cancels the pending retry, and so does close().
  • Remote eval is not retried: responses are per user, so preload_remote_eval() and the sync remote-eval load make one attempt; evaluations fall back to defaults and refetch on their own cache miss.
  • New GrowthBookClient.refresh_features(force_refresh=True):
    • Bypasses the cache and returns whether features were applied.
    • After a failed initialize(), a successful call completes the initialization and starts background refresh.
    • In remote-eval mode, with force_refresh=True it drops the cached per-user responses.
  • Options: max_fetch_retries (default 5, 0 disables) and fetch_retry_max_delay (default 16s, must be finite), on both GrowthBook(...) and Options, validated at construction. Both are appended after request_timeout, so existing positional arguments keep their meaning.
  • README: retry section and "Forcing a refresh".

Shares the callback routing change with #173. The code is identical in both, so whichever PR lands second merges cleanly there.

How to test

pytest tests/test_fetch_retry.py tests/test_request_timeout.py

42 new tests in tests/test_fetch_retry.py:

  • the retry schedule and its logs;
  • transient vs non-retryable failures, including 4xx stopping a running loop;
  • one loop per key, finished loops dropped, results reaching only that key's instances, instances on other keys not keeping the loop alive;
  • destroyed instances not receiving results, including updates already being delivered;
  • async initialize() retry, cancellation on a later successful initialize() and on close();
  • refresh_features, including completing a failed initialize();
  • remote eval not retried, option validation including NaN and infinity.

test_request_timeout.py now checks that all new fields come after the released ones.

Full suite (with #169 and current main merged): 2034 passed. The 2 mypy failures in test_typing.py come from the local urllib3 version and happen without these changes too.

Size

Relative to #169: production code +549 / −52, tests +789 / −12, README +62. About 1,400 lines, more than half of it tests.

Migration

None. Retries start only after a failed fetch and can be turned off with max_fetch_retries=0. add_feature_update_callback(callback) without a key behaves as before.

…sh_features

A failed feature fetch was never retried by the SDK: the only retries were
urllib3's immediate ones (disabled by the timeout change), 5xx and
remote-eval POSTs got none, and the async initialize() made one attempt.

- After a failed fetch the call returns at once with the best available
  features, and the fetch is retried in the background with delays of
  1, 2, 4, 8, 16s (max_fetch_retries=5, fetch_retry_max_delay=16; 0 disables).
  Each retry logs "Feature fetch failed. Retry attempt n/N in Xms."; after the
  last one the SDK stops and keeps what it has. Retries never extend startup.
- Sync GrowthBook: one daemon thread per instance at a time, stopped by
  destroy(). Covers constructor loads, load_features(), SSE-triggered and
  remote-eval loads; remote-eval retries rebuild the payload each attempt.
  The lazy per-evaluation refresh doesn't start retries, since the next
  evaluation already re-fetches.
- Async GrowthBookClient: a failed initialize() still returns False; an
  asyncio task retries and, on success, installs the features and starts
  refreshing like a successful initialize(). close() cancels it.
  preload_remote_eval() retries in place, since its response is per user.
- New GrowthBookClient.refresh_features(force_refresh=True): bypasses the
  cache and applies the result; in remote-eval mode it drops cached per-user
  responses. Sync already has load_features(force_refresh=True).
- Options are validated at construction; new constructor parameters are
  appended last.
- Only connection errors, timeouts, 429 and 5xx are retried. The repository
  records the failure kind; a 401/403/404, an unusable body or a decryption
  failure is reported once, and stops a retry loop already running. Same in
  the async client's init retry; an exception from initialize() (a
  configuration error) is no longer retried either.
- The sync retry loop moved into the shared feature_repo: one loop per
  (api_host, client_key), delivering through the feature-update callbacks.
  Per instance, an outage multiplied requests by the number of instances and
  each loop kept its instance alive. The loop stops early once every
  instance is destroyed.
- preload_remote_eval() makes one attempt instead of retrying in place,
  which added the whole schedule to every preloading request during an
  outage. Sync remote eval isn't retried in the background either; both
  fall back to defaults and refetch on the next cache miss.
- max_fetch_retries and fetch_retry_max_delay are appended after
  request_timeout so existing positional Options arguments keep their
  meaning; the field-order test now pins the whole released prefix.
…sh init from refresh_features

- A retry success went through load_features, which notified every
  connection in the process, so other keys' instances got this key's flags.
  Route feature updates by (api_host, client_key), the same change as in
  feat/polling-interval: GrowthBook registers its callback with its key and
  loads/pollers notify with theirs; unkeyed callbacks still hear everything.
- The retry loop kept going while any instance in the process was alive.
  It now stops once nothing on its key (or unkeyed) would receive the result.
- refresh_features() after a failed initialize() applied the features but
  never registered the update callback or started refreshing. It now
  completes the initialization and cancels the pending retry. In remote-eval
  mode it returns False when nothing was flushed.
@vazarkevych vazarkevych changed the title Feat/fetch retry feat: bounded background retry of failed feature fetches, async refresh_features Dec 3, 2026
@greptile-apps

greptile-apps Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Confidence Score: 5/5RetriggerView in Greptile

[Medium impact] The reviewed changes appear safe to merge; all four previous findings are addressed.

Reviews (2) · Last reviewed commit: "fix: forget finished retry workers, canc..." · Reviewed by Greptile

Comment thread growthbook/growthbook.py Resolved
Comment thread growthbook/growthbook_client.py Resolved
Comment thread growthbook/growthbook.py Resolved
Comment thread growthbook/common_types.py Resolved
…ore updates after destroy

- A finished fetch retry worker removes its own entry, so keys that
  once failed don't accumulate threads until stop_fetch_retries().
- Any successful initialization path (initialize(), the retry,
  refresh_features()) cancels a pending initial-load retry.
- destroy() and feature-update delivery agree on liveness through the
  payload lock, so an update dispatched before destroy() can't
  repopulate the instance.
- fetch_retry_max_delay rejects NaN and infinity.
@vazarkevych

Copy link
Copy Markdown
Collaborator Author

@greptile review

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