Repository navigation
fix: apply http_connect_timeout/http_read_timeout to async requests #168
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
pcbeingused33
wants to merge
3
commits into
growthbook:main
from
pcbeingused33:fix/async-http-timeouts
Closed
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| """Options.http_connect_timeout / http_read_timeout must reach every HTTP path.""" | ||
| from unittest.mock import patch | ||
|
|
||
| import aiohttp | ||
| import pytest | ||
|
|
||
| from growthbook.growthbook import FeatureRepository | ||
|
|
||
|
|
||
| class _FakeResponse: | ||
| status = 500 | ||
|
|
||
| async def __aenter__(self): | ||
| return self | ||
|
|
||
| async def __aexit__(self, *exc): | ||
| return False | ||
|
|
||
|
|
||
| class _FakeSession: | ||
| """Stands in for aiohttp.ClientSession and records its constructor kwargs.""" | ||
|
|
||
| created = [] | ||
|
|
||
| def __init__(self, **kwargs): | ||
| self.kwargs = kwargs | ||
| _FakeSession.created.append(self) | ||
|
|
||
| async def __aenter__(self): | ||
| return self | ||
|
|
||
| async def __aexit__(self, *exc): | ||
| return False | ||
|
|
||
| def get(self, *args, **kwargs): | ||
| return _FakeResponse() | ||
|
|
||
| def post(self, *args, **kwargs): | ||
| return _FakeResponse() | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def fake_session(): | ||
| _FakeSession.created = [] | ||
| with patch("growthbook.growthbook.aiohttp.ClientSession", _FakeSession): | ||
| yield _FakeSession | ||
|
|
||
|
|
||
| def _repo(connect=None, read=None): | ||
| repo = FeatureRepository() | ||
| repo.http_connect_timeout = connect | ||
| repo.http_read_timeout = read | ||
| return repo | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_async_get_applies_http_timeouts(fake_session): | ||
| repo = _repo(connect=3, read=7) | ||
|
|
||
| assert await repo._fetch_and_decode_async("https://cdn.growthbook.io", "sdk-abc123") is None | ||
|
|
||
| (session,) = fake_session.created | ||
| timeout = session.kwargs["timeout"] | ||
| assert isinstance(timeout, aiohttp.ClientTimeout) | ||
| assert timeout.sock_connect == 3 | ||
| assert timeout.sock_read == 7 | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_async_post_applies_http_timeouts(fake_session): | ||
| repo = _repo(connect=3, read=7) | ||
|
|
||
| assert await repo._fetch_and_decode_post_async( | ||
| "https://cdn.growthbook.io", "sdk-abc123", {"attributes": {}} | ||
| ) is None | ||
|
|
||
| (session,) = fake_session.created | ||
| timeout = session.kwargs["timeout"] | ||
| assert timeout.sock_connect == 3 | ||
| assert timeout.sock_read == 7 | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| @pytest.mark.parametrize("connect, read", [(3, 7), (3, None), (None, 7)]) | ||
| async def test_async_keeps_aiohttp_defaults_for_unset_fields(fake_session, connect, read): | ||
| default = aiohttp.client.DEFAULT_TIMEOUT | ||
| repo = _repo(connect=connect, read=read) | ||
|
|
||
| await repo._fetch_and_decode_async("https://cdn.growthbook.io", "sdk-abc123") | ||
|
|
||
| timeout = fake_session.created[0].kwargs["timeout"] | ||
| # The overall request limit must survive setting either option. | ||
| assert timeout.total == default.total | ||
| assert timeout.connect == default.connect | ||
| assert timeout.sock_connect == (connect if connect is not None else default.sock_connect) | ||
| assert timeout.sock_read == (read if read is not None else default.sock_read) | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_async_keeps_aiohttp_defaults_without_timeouts(fake_session): | ||
| repo = _repo() | ||
|
|
||
| await repo._fetch_and_decode_async("https://cdn.growthbook.io", "sdk-abc123") | ||
|
|
||
| assert "timeout" not in fake_session.created[0].kwargs | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "connect, read", | ||
| [(3, 7), (3, None), (None, 7)], | ||
| ) | ||
| def test_sync_pool_manager_applies_each_timeout(connect, read): | ||
| repo = _repo(connect=connect, read=read) | ||
|
|
||
| with patch("growthbook.growthbook.PoolManager") as pool_manager: | ||
| repo._get("https://cdn.growthbook.io/api/features/sdk-abc123") | ||
|
|
||
| timeout = pool_manager.call_args.kwargs["timeout"] | ||
| assert timeout.connect_timeout == connect | ||
| assert timeout.read_timeout == read | ||
|
|
||
|
|
||
| def test_sync_pool_manager_has_no_timeout_by_default(): | ||
| repo = _repo() | ||
|
|
||
| with patch("growthbook.growthbook.PoolManager") as pool_manager: | ||
| repo._get("https://cdn.growthbook.io/api/features/sdk-abc123") | ||
|
|
||
| assert pool_manager.call_args.kwargs["timeout"] is None |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new one-sided timeout support still fails when
GrowthBookloads during construction. With a freshfeature_repo, enablingstreaming,stale_while_revalidate, orremote_evalcallsload_features()before these assignments. That request createsPoolManager(timeout=None). Later requests reuse the same pool, so the configured timeout never applies and requests can wait indefinitely.Move these assignments before plugins and automatic loads, and add a constructor-level test.
Knowledge Base Used: Client lifecycle and feature refresh
Prompt To Fix With AI