-
Notifications
You must be signed in to change notification settings - Fork 1.7k
fix(vars): use monotonic time for computed var intervals #6768
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Use monotonic time for computed var update intervals so system clock changes do not cause early or delayed cache refreshes. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,8 @@ | ||
| import datetime | ||
| from collections.abc import Mapping, Sequence | ||
|
|
||
| import pytest | ||
| from pytest_mock import MockerFixture | ||
| from reflex_base.vars.base import computed_var, figure_out_type | ||
|
|
||
| from reflex.state import State | ||
|
|
@@ -77,3 +79,100 @@ def cv(self) -> int: | |
|
|
||
| replaced = cv._replace(_var_type=float) | ||
| assert replaced._var_type is float | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("is_async", [False, True]) | ||
| @pytest.mark.parametrize( | ||
| ("wall_clock_delta", "monotonic_delta", "expected_value"), | ||
| [ | ||
| (datetime.timedelta(hours=-1), 11, 2), | ||
| (datetime.timedelta(hours=1), 1, 1), | ||
| ], | ||
| ) | ||
| @pytest.mark.asyncio | ||
| async def test_computed_var_interval_uses_monotonic_time( | ||
| mocker: MockerFixture, | ||
| is_async: bool, | ||
| wall_clock_delta: datetime.timedelta, | ||
| monotonic_delta: int, | ||
| expected_value: int, | ||
| ) -> None: | ||
| """Test interval expiry is unaffected by wall-clock changes. | ||
|
|
||
| Args: | ||
| mocker: Pytest mock fixture. | ||
| is_async: Whether to access the asynchronous computed var. | ||
| wall_clock_delta: The wall-clock change after the initial access. | ||
| monotonic_delta: The elapsed monotonic time after the initial access. | ||
| expected_value: The expected computed value after the clocks advance. | ||
| """ | ||
| call_count = 0 | ||
|
|
||
| class StateTest(State): | ||
| @computed_var(cache=True, interval=10) | ||
| def cv(self) -> int: | ||
| nonlocal call_count | ||
| call_count += 1 | ||
| return call_count | ||
|
|
||
| @computed_var(cache=True, interval=10) | ||
| async def async_cv(self) -> int: | ||
| nonlocal call_count | ||
| call_count += 1 | ||
| return call_count | ||
|
|
||
| monotonic_time = 100 | ||
| mocker.patch( | ||
| "reflex_base.vars.base.time.monotonic", side_effect=lambda: monotonic_time | ||
| ) | ||
| initial_wall_time = datetime.datetime(2026, 1, 1) | ||
| datetime_mock = mocker.patch("reflex_base.vars.base.datetime.datetime") | ||
| datetime_mock.now.return_value = initial_wall_time | ||
|
|
||
| state = StateTest() | ||
| if is_async: | ||
| assert await state.async_cv == 1 | ||
| else: | ||
| assert state.cv == 1 | ||
|
|
||
| monotonic_time += monotonic_delta | ||
| datetime_mock.now.return_value = initial_wall_time + wall_clock_delta | ||
|
Comment on lines
+129
to
+139
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time! |
||
|
|
||
| if is_async: | ||
| assert await state.async_cv == expected_value | ||
| else: | ||
| assert state.cv == expected_value | ||
| assert call_count == expected_value | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "persisted_timestamp", | ||
| [ | ||
| datetime.datetime(2026, 1, 1), | ||
| (object(), 100), | ||
| ], | ||
| ) | ||
| def test_computed_var_interval_expires_incompatible_timestamp( | ||
| persisted_timestamp: object, | ||
| ) -> None: | ||
| """Test an interval cache with an incompatible timestamp is expired. | ||
|
|
||
| Args: | ||
| persisted_timestamp: A timestamp from a legacy version or another clock. | ||
| """ | ||
| call_count = 0 | ||
|
|
||
| class StateTest(State): | ||
| @computed_var(cache=True, interval=10) | ||
| def cv(self) -> int: | ||
| nonlocal call_count | ||
| call_count += 1 | ||
| return call_count | ||
|
|
||
| state = StateTest() | ||
| assert state.cv == 1 | ||
|
|
||
| computed = StateTest.computed_vars["cv"] | ||
| setattr(state, computed._last_updated_attr, persisted_timestamp) | ||
|
|
||
| assert state.cv == 2 | ||
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.
elapsed < 0is unreachable once the UUID guard passes. Within a single process,time.monotonic()is guaranteed to be non-decreasing, so subtracting a previously recorded value from the same process's clock will always yield a non-negative result. Consider removing the dead condition to keep the expression clean.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!