Skip to content

Commit

Permalink
[Core] Make "GetTimeoutError" a subclass of "TimeoutError" (ray-proje…
Browse files Browse the repository at this point in the history
…ct#26771)

I am surprised by the fact that `GetTimeoutError` is not a subclass of `TimeoutError`, which is counter-intuitive and may discourage users from trying the timeout feature in `ray.get`, because you have to "guess" the correct error type. For most people, I believe the first error type in their mind would be `TimeoutError`.

This PR fixes this.
  • Loading branch information
suquark committed Jul 20, 2022
1 parent 8a45425 commit 0063d94
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 3 deletions.
4 changes: 2 additions & 2 deletions doc/source/ray-core/objects.rst
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,16 @@ If the current node's object store does not contain the object, the object is do
# You can also set a timeout to return early from a ``get``
# that's blocking for too long.
from ray.exceptions import GetTimeoutError
# ``GetTimeoutError`` is a subclass of ``TimeoutError``.
@ray.remote
def long_running_function():
time.sleep(8)
obj_ref = long_running_function.remote()
try:
ray.get(obj_ref, timeout=4)
except GetTimeoutError:
except GetTimeoutError: # You can capture the standard "TimeoutError" instead
print("`get` timed out.")
.. tabbed:: Java
Expand Down
2 changes: 1 addition & 1 deletion python/ray/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ def __str__(self):


@PublicAPI
class GetTimeoutError(RayError):
class GetTimeoutError(RayError, TimeoutError):
"""Indicates that a call to the worker timed out."""

pass
Expand Down
4 changes: 4 additions & 0 deletions python/ray/tests/test_basic_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,10 @@ def test_get_with_timeout(ray_start_regular_shared):
with pytest.raises(GetTimeoutError):
ray.get(result_id, timeout=0.1)

assert issubclass(GetTimeoutError, TimeoutError)
with pytest.raises(TimeoutError):
ray.get(result_id, timeout=0.1)

# Check that a subsequent get() returns early.
ray.get(signal.send.remote())
start = time.time()
Expand Down

0 comments on commit 0063d94

Please sign in to comment.