File tree Expand file tree Collapse file tree 3 files changed +22
-3
lines changed Expand file tree Collapse file tree 3 files changed +22
-3
lines changed Original file line number Diff line number Diff line change 97
97
ReflexRuntimeError ,
98
98
SetUndefinedStateVarError ,
99
99
StateSchemaMismatchError ,
100
+ StateSerializationError ,
100
101
StateTooLargeError ,
101
102
)
102
103
from reflex .utils .exec import is_testing_env
@@ -2193,8 +2194,12 @@ def _serialize(self) -> bytes:
2193
2194
2194
2195
Returns:
2195
2196
The serialized state.
2197
+
2198
+ Raises:
2199
+ StateSerializationError: If the state cannot be serialized.
2196
2200
"""
2197
2201
payload = b""
2202
+ error = ""
2198
2203
try :
2199
2204
payload = pickle .dumps ((self ._to_schema (), self ))
2200
2205
except HANDLED_PICKLE_ERRORS as og_pickle_error :
@@ -2214,8 +2219,13 @@ def _serialize(self) -> bytes:
2214
2219
except HANDLED_PICKLE_ERRORS as ex :
2215
2220
error += f"Dill was also unable to pickle the state: { ex } "
2216
2221
console .warn (error )
2222
+
2217
2223
if environment .REFLEX_PERF_MODE .get () != PerformanceMode .OFF :
2218
2224
self ._check_state_size (len (payload ))
2225
+
2226
+ if not payload :
2227
+ raise StateSerializationError (error )
2228
+
2219
2229
return payload
2220
2230
2221
2231
@classmethod
Original file line number Diff line number Diff line change @@ -155,6 +155,10 @@ class StateTooLargeError(ReflexError):
155
155
"""Raised when the state is too large to be serialized."""
156
156
157
157
158
+ class StateSerializationError (ReflexError ):
159
+ """Raised when the state cannot be serialized."""
160
+
161
+
158
162
class SystemPackageMissingError (ReflexError ):
159
163
"""Raised when a system package is missing."""
160
164
Original file line number Diff line number Diff line change 55
55
)
56
56
from reflex .testing import chdir
57
57
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
+ )
59
63
from reflex .utils .format import json_dumps
60
64
from reflex .vars .base import Var , computed_var
61
65
from tests .units .states .mutation import MutableSQLAModel , MutableTestState
@@ -3433,8 +3437,9 @@ class DillState(BaseState):
3433
3437
# Some object, like generator, are still unpicklable with dill.
3434
3438
state3 = DillState (_reflex_internal_init = True ) # type: ignore
3435
3439
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 ()
3438
3443
3439
3444
3440
3445
def test_typed_state () -> None :
You can’t perform that action at this time.
0 commit comments