Skip to content

feat: configurable, finite HTTP request timeouts - #169

Open
vazarkevych wants to merge 7 commits into
mainfrom
feat/request-timeout
Open

vazarkevych wants to merge 7 commits into
mainfrom
feat/request-timeout

Conversation

@vazarkevych

@vazarkevych vazarkevych commented Nov 30, 2026 •

Copy link
Copy Markdown
Collaborator

What and why

A feature fetch could block forever: with no timeout options set, the sync client sent requests with no connect or read limit, so load_features() never returned against a slow or unreachable endpoint. The existing options were unreliable too: they only worked when both were set, 0 silently disabled them, and the async client ignored them.

What changed

  • New request_timeout option (seconds, default 2) for the sync and async clients, covering feature fetches and remote-eval. It bounds each connect and read phase, not total wall-clock time. On timeout the SDK falls back to cache or an inline payload. http_connect_timeout / http_read_timeout now work on their own as per-phase overrides.
  • Async: requests use the resolved values (sock_connect / sock_read, plus connect so a stalled DNS lookup is bounded too). aiohttp's overall limit stays as a backstop unless both phases are bounded.
  • Validation: 0, negative, NaN, infinity and non-numbers are rejected at construction on both clients. Pass None to opt out.
  • No retries on timeout: urllib3's default retries made the real wait about 4× the configured timeout. Redirects are still followed. Retries are disabled only on the pool the SDK creates. A pool you assign to feature_repo.http keeps its own retry policy.
  • Per-instance timeouts: each GrowthBook keeps its own instead of overwriting the shared feature_repo.
  • New parameters are appended to GrowthBook(...) and Options, so positional calls keep working.

Breaking change

The default changes from no limit (sync) and 300s (async) to 2s. To keep the old behavior, use GrowthBook(request_timeout=None) or GrowthBookClient(Options(request_timeout=None)) (the async client then keeps aiohttp's 300s limit). For sync clients only, feature_repo.request_timeout = None sets it once for the whole process. The README has an upgrade note.

Dependency

urllib3>=1.26.0 in setup.py and pyproject.toml, needed for the no-retry setting. requirements.txt already required it. No new packages.

How to test

pytest tests/test_request_timeout.py

21 tests, including a server that accepts connections and never answers. With a 0.5s timeout a fetch used to take 2.01s over 4 connections; now it takes 0.55s over 1.

Full suite with current main merged: 1965 passed. The 2 cache fixture errors in test_growthbook_client.py also happen on main.

Size

Production code +287 / −51, tests +314, README +52.

Related

Supersedes #168 (closed): same async fix plus the finite default, validation and the retry fix. Thanks to its author: keeping aiohttp's overall limit as a backstop came from there.

A feature fetch could block forever. The sync transport built
`PoolManager(timeout=None)` whenever the timeout options were unset, which
urllib3 resolves to `Timeout(connect=None, read=None, total=None)` — no bound
at all. Against a slow or black-hole endpoint, `load_features()` never
returned and the caller had no way to cap it. Reproduced against a socket that
accepts and then stays silent: the fetch was still blocked after 8 seconds.

Adds `request_timeout` (seconds, connect and read alike) defaulting to 2, so an
unconfigured SDK is bounded and falls back to cache or an inline payload
instead of hanging. `None` opts back into waiting forever.

Four further defects in the same area:

Each override had to be set with the other. `if connect and read` meant
configuring just one silently applied neither; they now resolve independently
and inherit `request_timeout` when absent.

That same falsy check made `0` disable both timeouts. Non-positive and
non-numeric values are now rejected — `timeout=0` reads as "no timeout" in
several HTTP stacks, so accepting it would turn a typo back into the unbounded
wait this default exists to prevent. Validation runs at construction, not at
first request.

The async fetches built a bare `aiohttp.ClientSession()`, so these options
never reached them and aiohttp's own 300s default applied — bounded, but
neither configurable nor a sensible startup budget. They now use the same
resolved values (`sock_connect` / `sock_read`; `total` would also cut a
legitimately large payload).

The pool was cached on first use, so the first request's timeout stuck for the
process lifetime, and the sync client applied the options *after* any
constructor-time load. The pool is now rebuilt when the configuration changes,
and the options are applied before the initial fetch.

tests/test_request_timeout.py covers the bound against a black-hole endpoint,
the finite default, independent overrides, rejection of 0 and of non-numbers,
fail-fast construction, and pool rebuilding.
- Disable urllib3 retries for feature and remote-eval requests. The default
  Retry(3) re-sent a timed-out GET, so the real bound was ~4x the timeout
  (2.01s and 4 connections for 0.5s; now 0.55s and 1). Redirects are still
  followed; Retry-After is not honored.
- GrowthBook() no longer writes its timeouts into the process-wide
  feature_repo. They stay on the instance and are applied for the duration
  of its own calls through a context variable, falling back field by field
  to feature_repo's attributes, so direct repo configuration keeps working
  and the last-constructed instance no longer wins. The SWR poller thread
  inherits the timeouts of the instance that started it.
- Never replace self.http: the pool is created once if absent, and timeouts
  go per request, so an injected ProxyManager or custom-CA pool is kept.
- request_timeout moves to the end of GrowthBook.__init__ so existing
  positional call sites keep their meaning. Not passing it (UNSET) means
  "use feature_repo.request_timeout"; None still opts out.
- README: per-attempt bound, per-instance scope, injected pools, and an
  upgrade note. DEFAULT_REQUEST_TIMEOUT comment no longer claims parity
  with other SDKs.
The feature and remote-eval requests use Retry(other=0), and the `other`
parameter only exists since urllib3 1.26. setup.py and pyproject.toml
accepted any urllib3, so an older pinned version failed at import time.
requirements.txt already required >=1.26.0.
- Async: keep aiohttp's overall limit unless both phases are bounded.
  request_timeout=None used to build a ClientTimeout with no limit at all,
  and opting out of only one phase left that phase unbounded.
- Validate GrowthBookClient Options at construction. A bad value used to
  raise inside the first fetch, where it was swallowed.
- Validate GrowthBook timeouts before any side effect (callback
  registration, plugin startup), and name feature_repo in the error when
  the bad value is inherited from it.
- Disable retries only on the pool the SDK creates; an injected pool keeps
  its own retry policy. Release the SDK's pool once it is replaced.
- README: document the opt-out bounds, injected-pool retries, the shared
  background poller and the async repository singleton.
- Tests: per-phase async timeouts, an async black-hole bound, fail-fast
  validation, injected-pool retries and pool release.
@greptile-apps

greptile-apps Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

Confidence Score: 5/5RetriggerView in Greptile

[Medium impact] The PR appears safe to merge; the previous upgrade-advice finding is fixed.

Reviews (4) · Last reviewed commit: "docs: fix async opt-out advice in the re..." · Reviewed by Greptile

Comment thread growthbook/growthbook.py Resolved
Comment thread growthbook/common_types.py Resolved
Comment thread growthbook/common_types.py Resolved
@vazarkevych

Copy link
Copy Markdown
Collaborator Author

@greptile review

@vazarkevych

Copy link
Copy Markdown
Collaborator Author

@greptile review

Comment thread README.md Resolved
feature_repo.request_timeout = None doesn't reach GrowthBookClient: its
repository takes Options.request_timeout, which still defaults to 2s.
Show Options(request_timeout=None) for the async client and keep the
shared setting as a sync-only option.
@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