Repository navigation
feat: configurable, finite HTTP request timeouts - #169
Open
vazarkevych wants to merge 7 commits into
Open
vazarkevych wants to merge 7 commits into
vazarkevych wants to merge 7 commits into
Conversation
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.
Confidence Score: 5/5
[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 |
Collaborator
Author
|
@greptile review |
Collaborator
Author
|
@greptile review |
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.
Collaborator
Author
|
@greptile review |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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,0silently disabled them, and the async client ignored them.What changed
request_timeoutoption (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_timeoutnow work on their own as per-phase overrides.sock_connect/sock_read, plusconnectso a stalled DNS lookup is bounded too). aiohttp's overall limit stays as a backstop unless both phases are bounded.0, negative, NaN, infinity and non-numbers are rejected at construction on both clients. PassNoneto opt out.feature_repo.httpkeeps its own retry policy.GrowthBookkeeps its own instead of overwriting the sharedfeature_repo.GrowthBook(...)andOptions, 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)orGrowthBookClient(Options(request_timeout=None))(the async client then keeps aiohttp's 300s limit). For sync clients only,feature_repo.request_timeout = Nonesets it once for the whole process. The README has an upgrade note.Dependency
urllib3>=1.26.0insetup.pyandpyproject.toml, needed for the no-retry setting.requirements.txtalready required it. No new packages.How to test
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
mainmerged: 1965 passed. The 2cachefixture errors intest_growthbook_client.pyalso happen onmain.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.