forked from ray-project/ray
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[core] out of band serialization exception (ray-project#47544)
Introduce an env var to raise an exception when there's out of band seriailzation of object ref Improve error message on out of band serialization issue. There are 2 types of issues. 1. cloudpikcle.dumps(ref). 2. implicit capture. See below for more details. Update an anti-pattern doc. Signed-off-by: ujjawal-khare <[email protected]>
- Loading branch information
1 parent
0114c6c
commit b83f63d
Showing
8 changed files
with
225 additions
and
22 deletions.
There are no files selected for viewing
64 changes: 64 additions & 0 deletions
64
doc/source/ray-core/doc_code/anti_pattern_out_of_band_object_ref_serialization.py
This file contains 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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# __anti_pattern_start__ | ||
import ray | ||
import pickle | ||
from ray._private.internal_api import memory_summary | ||
import ray.exceptions | ||
|
||
ray.init() | ||
|
||
|
||
@ray.remote | ||
def out_of_band_serialization_pickle(): | ||
obj_ref = ray.put(1) | ||
import pickle | ||
|
||
# object_ref is serialized from user code using a regular pickle. | ||
# Ray can't keep track of the reference, so the underlying object | ||
# can be GC'ed unexpectedly, which can cause unexpected hangs. | ||
return pickle.dumps(obj_ref) | ||
|
||
|
||
@ray.remote | ||
def out_of_band_serialization_ray_cloudpickle(): | ||
obj_ref = ray.put(1) | ||
from ray import cloudpickle | ||
|
||
# ray.cloudpickle can serialize only when | ||
# RAY_allow_out_of_band_object_ref_serialization=1 env var is set. | ||
# However, the object_ref is pinned for the lifetime of the worker, | ||
# which can cause Ray object leaks that can cause spilling. | ||
return cloudpickle.dumps(obj_ref) | ||
|
||
|
||
print("==== serialize object ref with pickle ====") | ||
result = ray.get(out_of_band_serialization_pickle.remote()) | ||
try: | ||
ray.get(pickle.loads(result), timeout=5) | ||
except ray.exceptions.GetTimeoutError: | ||
print("Underlying object is unexpectedly GC'ed!\n\n") | ||
|
||
print("==== serialize object ref with ray.cloudpickle ====") | ||
# By default, it's allowed to serialize ray.ObjectRef using | ||
# ray.cloudpickle. | ||
ray.get(out_of_band_serialization_ray_cloudpickle.options().remote()) | ||
# you can see objects are stil pinned although it's GC'ed and not used anymore. | ||
print(memory_summary()) | ||
|
||
print( | ||
"==== serialize object ref with ray.cloudpickle with env var " | ||
"RAY_allow_out_of_band_object_ref_serialization=0 for debugging ====" | ||
) | ||
try: | ||
ray.get( | ||
out_of_band_serialization_ray_cloudpickle.options( | ||
runtime_env={ | ||
"env_vars": { | ||
"RAY_allow_out_of_band_object_ref_serialization": "0", | ||
} | ||
} | ||
).remote() | ||
) | ||
except Exception as e: | ||
print(f"Exception raised from out_of_band_serialization_ray_cloudpickle {e}\n\n") | ||
|
||
# __anti_pattern_end__ |
This file contains 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 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
26 changes: 26 additions & 0 deletions
26
doc/source/ray-core/patterns/out-of-band-object-ref-serialization.rst
This file contains 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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
.. _ray-out-of-band-object-ref-serialization: | ||
|
||
Anti-pattern: Serialize ray.ObjectRef out of band | ||
================================================= | ||
|
||
**TLDR:** Avoid serializing ``ray.ObjectRef`` because Ray can't know when to garbage collect the underlying object. | ||
|
||
Ray's ``ray.ObjectRef`` is distributed reference counted. Ray pins the underlying object until the reference isn't used by the system anymore. | ||
When all references are the pinned object gone, Ray garbage collects the pinned object and cleans it up from the system. | ||
However, if user code serializes ``ray.objectRef``, Ray can't keep track of the reference. | ||
|
||
To avoid incorrect behavior, if ``ray.cloudpickle`` serializes``ray.ObjectRef``, Ray pins the object for the lifetime of a worker. "Pin" means that object can't be evicted from the object store | ||
until the corresponding owner worker dies. It's prone to Ray object leaks, which can lead disk spilling. See :ref:`thjs page <serialize-object-ref>` for more details. | ||
|
||
To detect if this pattern exists in your code, you can set an environment variable ``RAY_allow_out_of_band_object_ref_serialization=0``. If Ray detects | ||
that ``ray.cloudpickle`` serialized``ray.ObjectRef``, it raises an exception with helpful messages. | ||
|
||
Code example | ||
------------ | ||
|
||
**Anti-pattern:** | ||
|
||
.. literalinclude:: ../doc_code/anti_pattern_out_of_band_object_ref_serialization.py | ||
:language: python | ||
:start-after: __anti_pattern_start__ | ||
:end-before: __anti_pattern_end__ |
This file contains 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 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 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 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