Skip to content

Commit 2ee201b

Browse files
raise StateSerializationError if the state cannot be serialized (#4453)
* raise StateSerializationError if the state cannot be serialized * fix test
1 parent 862d7ec commit 2ee201b

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

reflex/state.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
ReflexRuntimeError,
9898
SetUndefinedStateVarError,
9999
StateSchemaMismatchError,
100+
StateSerializationError,
100101
StateTooLargeError,
101102
)
102103
from reflex.utils.exec import is_testing_env
@@ -2193,8 +2194,12 @@ def _serialize(self) -> bytes:
21932194
21942195
Returns:
21952196
The serialized state.
2197+
2198+
Raises:
2199+
StateSerializationError: If the state cannot be serialized.
21962200
"""
21972201
payload = b""
2202+
error = ""
21982203
try:
21992204
payload = pickle.dumps((self._to_schema(), self))
22002205
except HANDLED_PICKLE_ERRORS as og_pickle_error:
@@ -2214,8 +2219,13 @@ def _serialize(self) -> bytes:
22142219
except HANDLED_PICKLE_ERRORS as ex:
22152220
error += f"Dill was also unable to pickle the state: {ex}"
22162221
console.warn(error)
2222+
22172223
if environment.REFLEX_PERF_MODE.get() != PerformanceMode.OFF:
22182224
self._check_state_size(len(payload))
2225+
2226+
if not payload:
2227+
raise StateSerializationError(error)
2228+
22192229
return payload
22202230

22212231
@classmethod

reflex/utils/exceptions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,10 @@ class StateTooLargeError(ReflexError):
155155
"""Raised when the state is too large to be serialized."""
156156

157157

158+
class StateSerializationError(ReflexError):
159+
"""Raised when the state cannot be serialized."""
160+
161+
158162
class SystemPackageMissingError(ReflexError):
159163
"""Raised when a system package is missing."""
160164

tests/units/test_state.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,11 @@
5555
)
5656
from reflex.testing import chdir
5757
from reflex.utils import format, prerequisites, types
58-
from reflex.utils.exceptions import ReflexRuntimeError, SetUndefinedStateVarError
58+
from reflex.utils.exceptions import (
59+
ReflexRuntimeError,
60+
SetUndefinedStateVarError,
61+
StateSerializationError,
62+
)
5963
from reflex.utils.format import json_dumps
6064
from reflex.vars.base import Var, computed_var
6165
from tests.units.states.mutation import MutableSQLAModel, MutableTestState
@@ -3433,8 +3437,9 @@ class DillState(BaseState):
34333437
# Some object, like generator, are still unpicklable with dill.
34343438
state3 = DillState(_reflex_internal_init=True) # type: ignore
34353439
state3._g = (i for i in range(10))
3436-
pk3 = state3._serialize()
3437-
assert len(pk3) == 0
3440+
3441+
with pytest.raises(StateSerializationError):
3442+
_ = state3._serialize()
34383443

34393444

34403445
def test_typed_state() -> None:

0 commit comments

Comments
 (0)