-
Notifications
You must be signed in to change notification settings - Fork 1.8k
fix: deliver linked state updates to all clients #6934
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
Open
benedikt-bartscher
wants to merge
4
commits into
reflex-dev:main
Choose a base branch
from
benedikt-bartscher:shared-state-redis-fix
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Shared state updates now reach linked clients connected to other backend instances — the fan-out previously skipped any client whose websocket was not connected to the instance processing the event, so with redis and multiple workers only same-instance clients received live updates. |
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
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,111 @@ | ||
| """Unit tests for shared state fan-out to other linked clients.""" | ||
|
|
||
| import asyncio | ||
| import pickle | ||
| from contextlib import asynccontextmanager | ||
| from unittest.mock import AsyncMock, Mock, patch | ||
|
|
||
| import pytest | ||
|
|
||
| from reflex.istate.shared import _do_update_other_tokens | ||
| from reflex.state import State | ||
| from reflex.utils.token_manager import ( | ||
| LocalTokenManager, | ||
| RedisTokenManager, | ||
| SocketRecord, | ||
| ) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def mock_redis(): | ||
| """Create a mock Redis client. | ||
|
|
||
| Returns: | ||
| The mock Redis client. | ||
| """ | ||
| redis = AsyncMock() | ||
| redis.get = AsyncMock(return_value=None) | ||
| redis.get_connection_kwargs = Mock(return_value={"db": 0}) | ||
| return redis | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def redis_manager(mock_redis): | ||
| """Create a RedisTokenManager instance with mocked config. | ||
|
|
||
| Returns: | ||
| The RedisTokenManager instance. | ||
| """ | ||
| with patch("reflex_base.config.get_config") as mock_get_config: | ||
| mock_config = Mock() | ||
| mock_config.redis_token_expiration = 3600 | ||
| mock_get_config.return_value = mock_config | ||
|
|
||
| return RedisTokenManager(mock_redis) | ||
|
|
||
|
|
||
| def _mock_app(token_manager) -> tuple[Mock, list[str]]: | ||
| """Create a mock app recording the tokens passed to modify_state. | ||
|
|
||
| Returns: | ||
| The mock app and the list collecting modified token idents. | ||
| """ | ||
| modified_tokens: list[str] = [] | ||
|
|
||
| @asynccontextmanager | ||
| async def modify_state(token, previous_dirty_vars=None): | ||
| modified_tokens.append(token.ident) | ||
| yield Mock() | ||
|
|
||
| app = Mock() | ||
| app.modify_state = modify_state | ||
| app.event_namespace = Mock() | ||
| app.event_namespace._token_manager = token_manager | ||
| return app, modified_tokens | ||
|
|
||
|
|
||
| async def _run_update_other_tokens(app, affected_tokens: set[str]) -> None: | ||
| """Run _do_update_other_tokens against a mock app and await its tasks.""" | ||
| with patch("reflex_base.registry.RegistrationContext.get") as mock_get: | ||
| mock_get.return_value = Mock(app=app) | ||
| tasks = _do_update_other_tokens( | ||
| affected_tokens=affected_tokens, | ||
| previous_dirty_vars={}, | ||
| state_type=State, | ||
| ) | ||
| await asyncio.gather(*tasks) | ||
|
|
||
|
|
||
| async def test_update_other_tokens_local_manager(): | ||
| """With a LocalTokenManager, only locally connected tokens are updated.""" | ||
| manager = LocalTokenManager() | ||
| manager.token_to_socket["connected"] = SocketRecord( | ||
| instance_id=manager.instance_id, sid="sid1" | ||
| ) | ||
| app, modified_tokens = _mock_app(manager) | ||
|
|
||
| await _run_update_other_tokens(app, {"connected", "disconnected"}) | ||
|
|
||
| assert modified_tokens == ["connected"] | ||
|
|
||
|
|
||
| async def test_update_other_tokens_redis_cross_instance(redis_manager, mock_redis): | ||
| """Tokens connected to another instance are resolved via redis and updated.""" | ||
| redis_manager.token_to_socket["local"] = SocketRecord( | ||
| instance_id=redis_manager.instance_id, sid="sid1" | ||
| ) | ||
| foreign_record = SocketRecord(instance_id="other-instance", sid="sid2") | ||
| foreign_key = redis_manager._get_redis_key("foreign") | ||
| mock_redis.get.side_effect = lambda key: ( | ||
| pickle.dumps(foreign_record) if key == foreign_key else None | ||
| ) | ||
| app, modified_tokens = _mock_app(redis_manager) | ||
|
|
||
| await _run_update_other_tokens(app, {"local", "foreign", "disconnected"}) | ||
|
|
||
| assert sorted(modified_tokens) == ["foreign", "local"] | ||
| # The foreign socket record is cached locally for later emit_update routing. | ||
| assert redis_manager.token_to_socket["foreign"] == foreign_record | ||
| # Locally owned sockets are authoritative and never require a redis lookup. | ||
| local_key = redis_manager._get_redis_key("local") | ||
| assert local_key not in [call.args[0] for call in mock_redis.get.call_args_list] |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.