diff --git a/reflex/state.py b/reflex/state.py index a5b9f14e7..83c933885 100644 --- a/reflex/state.py +++ b/reflex/state.py @@ -3240,7 +3240,7 @@ class StateManagerRedis(StateManager): # The maximum time to hold a lock (ms). lock_expiration: int = pydantic.Field(default_factory=_default_lock_expiration) - # The minimum time to hold a lock (ms). + # The maximum time to hold a lock (ms) before warning. lock_warning_threshold: int = pydantic.Field( default_factory=_default_lock_warning_threshold ) @@ -3436,12 +3436,10 @@ class StateManagerRedis(StateManager): time_taken = self.lock_expiration / 1000 - ( await self.redis.ttl(self._lock_key(token)) ) - _validate_lock_warning_threshold( - self.lock_warning_threshold, self.lock_expiration - ) if time_taken > self.lock_warning_threshold / 1000: console.warn( - f"Lock for token {token} was held too long {time_taken=}s, avoid blocking operations.", + f"Lock for token {token} was held too long {time_taken=}s, " + f"use `@rx.event(background=True)` decorator for long-running tasks.", dedupe=True, ) diff --git a/tests/units/test_state.py b/tests/units/test_state.py index d79a070db..68bafe4df 100644 --- a/tests/units/test_state.py +++ b/tests/units/test_state.py @@ -3289,15 +3289,41 @@ async def test_setvar_async_setter(): @pytest.mark.parametrize( "expiration_kwargs, expected_values", [ - ({"redis_lock_expiration": 20000}, (20000, constants.Expiration.TOKEN)), + ( + {"redis_lock_expiration": 20000}, + ( + 20000, + constants.Expiration.TOKEN, + constants.Expiration.LOCK_WARNING_THRESHOLD, + ), + ), ( {"redis_lock_expiration": 50000, "redis_token_expiration": 5600}, - (50000, 5600), + (50000, 5600, constants.Expiration.LOCK_WARNING_THRESHOLD), ), - ({"redis_token_expiration": 7600}, (constants.Expiration.LOCK, 7600)), ( - {"redis_lock_expiration": 50000, "redis_lock_warning_threshold": 2000}, - (50000, 2000), + {"redis_token_expiration": 7600}, + ( + constants.Expiration.LOCK, + 7600, + constants.Expiration.LOCK_WARNING_THRESHOLD, + ), + ), + ( + {"redis_lock_expiration": 50000, "redis_lock_warning_threshold": 1500}, + (50000, constants.Expiration.TOKEN, 1500), + ), + ( + {"redis_token_expiration": 5600, "redis_lock_warning_threshold": 3000}, + (constants.Expiration.LOCK, 5600, 3000), + ), + ( + { + "redis_lock_expiration": 50000, + "redis_token_expiration": 5600, + "redis_lock_warning_threshold": 2000, + }, + (50000, 5600, 2000), ), ], ) @@ -3328,6 +3354,7 @@ config = rx.Config( state_manager = StateManager.create(state=State) assert state_manager.lock_expiration == expected_values[0] # type: ignore assert state_manager.token_expiration == expected_values[1] # type: ignore + assert state_manager.lock_warning_threshold == expected_values[2] # type: ignore @pytest.mark.skipif("REDIS_URL" not in os.environ, reason="Test requires redis")