From 5be7ce8a0db67264aa43d323b605c4d74b2e74d2 Mon Sep 17 00:00:00 2001 From: Benedikt Bartscher Date: Thu, 28 Nov 2024 21:46:35 +0100 Subject: [PATCH 1/2] raise StateSerializationError if the state cannot be serialized --- reflex/state.py | 10 ++++++++++ reflex/utils/exceptions.py | 4 ++++ 2 files changed, 14 insertions(+) diff --git a/reflex/state.py b/reflex/state.py index 55f29cf45f..899ce528d2 100644 --- a/reflex/state.py +++ b/reflex/state.py @@ -97,6 +97,7 @@ ReflexRuntimeError, SetUndefinedStateVarError, StateSchemaMismatchError, + StateSerializationError, StateTooLargeError, ) from reflex.utils.exec import is_testing_env @@ -2177,8 +2178,12 @@ def _serialize(self) -> bytes: Returns: The serialized state. + + Raises: + StateSerializationError: If the state cannot be serialized. """ payload = b"" + error = "" try: payload = pickle.dumps((self._to_schema(), self)) except HANDLED_PICKLE_ERRORS as og_pickle_error: @@ -2198,8 +2203,13 @@ def _serialize(self) -> bytes: except HANDLED_PICKLE_ERRORS as ex: error += f"Dill was also unable to pickle the state: {ex}" console.warn(error) + if environment.REFLEX_PERF_MODE.get() != PerformanceMode.OFF: self._check_state_size(len(payload)) + + if not payload: + raise StateSerializationError(error) + return payload @classmethod diff --git a/reflex/utils/exceptions.py b/reflex/utils/exceptions.py index 714dc912c7..a89c4d4aa1 100644 --- a/reflex/utils/exceptions.py +++ b/reflex/utils/exceptions.py @@ -155,6 +155,10 @@ class StateTooLargeError(ReflexError): """Raised when the state is too large to be serialized.""" +class StateSerializationError(ReflexError): + """Raised when the state cannot be serialized.""" + + class SystemPackageMissingError(ReflexError): """Raised when a system package is missing.""" From d93fae6a7c6825506862553642a308d96617f59e Mon Sep 17 00:00:00 2001 From: Benedikt Bartscher Date: Thu, 28 Nov 2024 21:57:50 +0100 Subject: [PATCH 2/2] fix test --- tests/units/test_state.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/tests/units/test_state.py b/tests/units/test_state.py index 8e61b8dae7..0121b23b10 100644 --- a/tests/units/test_state.py +++ b/tests/units/test_state.py @@ -55,7 +55,11 @@ ) from reflex.testing import chdir from reflex.utils import format, prerequisites, types -from reflex.utils.exceptions import ReflexRuntimeError, SetUndefinedStateVarError +from reflex.utils.exceptions import ( + ReflexRuntimeError, + SetUndefinedStateVarError, + StateSerializationError, +) from reflex.utils.format import json_dumps from reflex.vars.base import Var, computed_var from tests.units.states.mutation import MutableSQLAModel, MutableTestState @@ -3433,8 +3437,9 @@ class DillState(BaseState): # Some object, like generator, are still unpicklable with dill. state3 = DillState(_reflex_internal_init=True) # type: ignore state3._g = (i for i in range(10)) - pk3 = state3._serialize() - assert len(pk3) == 0 + + with pytest.raises(StateSerializationError): + _ = state3._serialize() def test_typed_state() -> None: