You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/source/ray-core/objects/serialization.rst
+57Lines changed: 57 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -202,6 +202,63 @@ There are at least 3 ways to define your custom serialization process:
202
202
except TypeError:
203
203
pass
204
204
205
+
.. _custom-exception-serializer:
206
+
207
+
Custom Serializers for Exceptions
208
+
----------------------------------
209
+
210
+
When Ray tasks raise exceptions that cannot be serialized with the default pickle mechanism, you can register custom serializers to handle them (Note: the serializer must be registered in the driver and all workers).
211
+
212
+
.. testcode::
213
+
214
+
import ray
215
+
import threading
216
+
217
+
class CustomError(Exception):
218
+
def __init__(self, message, data):
219
+
self.message = message
220
+
self.data = data
221
+
self.lock = threading.Lock() # Cannot be serialized
print(f"Caught exception: {e.cause}") # This will be our CustomError
253
+
254
+
When a custom exception is raised in a remote task, Ray will:
255
+
256
+
1. Serialize the exception using your custom serializer
257
+
2. Wrap it in a :class:`RayTaskError <ray.exceptions.RayTaskError>`
258
+
3. The deserialized exception will be available as ``ray_task_error.cause``
259
+
260
+
Whenever serialization fails, Ray throws an :class:`UnserializableException <ray.exceptions.UnserializableException>` containing the string representation of the original stack trace.
"Failed to deserialize exception. Refer to https://docs.ray.io/en/latest/ray-core/objects/serialization.html#troubleshooting to troubleshoot.\n"
930
+
"Failed to deserialize exception. Refer to https://docs.ray.io/en/latest/ray-core/objects/serialization.html#custom-serializers-for-exceptions for more information.\n"
Copy file name to clipboardExpand all lines: python/ray/tests/test_traceback.py
+38-1Lines changed: 38 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -301,7 +301,7 @@ def __repr__(self):
301
301
302
302
303
303
deftest_unpickleable_stacktrace(shutdown_only):
304
-
expected_output="""Failed to deserialize exception. Refer to https://docs.ray.io/en/latest/ray-core/objects/serialization.html#troubleshooting to troubleshoot.
304
+
expected_output="""Failed to deserialize exception. Refer to https://docs.ray.io/en/latest/ray-core/objects/serialization.html#custom-serializers-for-exceptions for more information.
expected_output_ray_put="""Could not serialize the put value <unlocked _thread.lock object at ADDRESS>:\nINSPECT_SERIALIZABILITY"""# noqa
335
372
expected_output_task="""Could not serialize the argument <unlocked _thread.lock object at ADDRESS> for a task or actor test_traceback.test_serialization_error_message.<locals>.task_with_unserializable_arg:\nINSPECT_SERIALIZABILITY"""# noqa
0 commit comments