-
Notifications
You must be signed in to change notification settings - Fork 6.8k
[core] Improve docs for custom serialization for exceptions + add test #56156
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jjyao
merged 31 commits into
ray-project:master
from
sampan-s-nayak:unserializable-exception2
Sep 10, 2025
Merged
Changes from all commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
35a147a
Introduce new exception type for un-pickleable exceptions
1140ad4
fix lint
bc48190
Merge branch 'master' into unserializable-exception
sampan-s-nayak de3e555
address comments
848eac5
Merge branch 'master' into unserializable-exception
sampan-s-nayak 9b42253
Add UnpickleableException to exceptions documentation
a7ad824
address comment
ef10264
address comment
702d96b
fix lint
5605deb
fix description
dae74d3
fix doc string indentation
5cac24d
custom exception serializer POC + test
03a440e
fix lint
3fd9c9a
clean up comments
132de6b
update comments and error message
6d457d2
fix lint
7abb43e
Merge branch 'master' into unserializable-exception
sampan-s-nayak e6f0443
minor change
ce53ff3
Merge remote-tracking branch 'origin/unserializable-exception' into u…
1e1582e
clean up code
da39a16
fix lint error
cff7b8e
Merge branch 'master' into unserializable-exception2
sampan-s-nayak 61b285b
tweak error message
b469def
improve docs for unserializable exceptions
ef12e32
fix lint
fb5cea8
improve docs
6c3442c
Merge branch 'master' into unserializable-exception2
sampan-s-nayak 5f70d5b
fix docs lint issue
51f316d
Merge remote-tracking branch 'origin/unserializable-exception2' into …
278c54e
address comment
a3ce312
address comments
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -301,7 +301,7 @@ def __repr__(self): | |
|
||
|
||
def test_unpickleable_stacktrace(shutdown_only): | ||
expected_output = """Failed to deserialize exception. Refer to https://docs.ray.io/en/latest/ray-core/objects/serialization.html#troubleshooting to troubleshoot. | ||
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. | ||
Original exception: | ||
ray.exceptions.RayTaskError: ray::f() (pid=XXX, ip=YYY) | ||
File "FILE", line ZZ, in f | ||
|
@@ -330,6 +330,43 @@ def f(): | |
assert clean_noqa(expected_output) == scrub_traceback(str(excinfo.value)) | ||
|
||
|
||
def test_exception_with_registered_serializer(shutdown_only): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this file can probably be converted to using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ill raise a separate pr for this |
||
class NoPickleError(OSError): | ||
def __init__(self, msg): | ||
self.msg = msg | ||
|
||
def __str__(self): | ||
return f"message: {self.msg}" | ||
|
||
def _serializer(e: NoPickleError): | ||
return {"msg": e.msg} | ||
|
||
def _deserializer(state): | ||
return NoPickleError(state["msg"] + " deserialized") | ||
|
||
@ray.remote | ||
def raise_custom_exception(): | ||
ray.util.register_serializer( | ||
NoPickleError, serializer=_serializer, deserializer=_deserializer | ||
) | ||
raise NoPickleError("message") | ||
|
||
try: | ||
with pytest.raises(NoPickleError) as exc_info: | ||
ray.get(raise_custom_exception.remote()) | ||
|
||
# Ensure dual-typed exception and message propagation | ||
assert isinstance(exc_info.value, RayTaskError) | ||
# if custom serializer was not registered, this would be an instance of UnserializableException() | ||
assert isinstance(exc_info.value, NoPickleError) | ||
assert "message" in str(exc_info.value) | ||
# modified message should not be in the exception string, only in the cause | ||
assert "deserialized" not in str(exc_info.value) | ||
assert "message deserialized" in str(exc_info.value.cause) | ||
finally: | ||
ray.util.deregister_serializer(NoPickleError) | ||
|
||
|
||
def test_serialization_error_message(shutdown_only): | ||
expected_output_ray_put = """Could not serialize the put value <unlocked _thread.lock object at ADDRESS>:\nINSPECT_SERIALIZABILITY""" # noqa | ||
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 | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Create a link here by doing
so that later on you can refer in the docstring.