# How to suppress TaskCancelledError without RAY\_IGNORE\_UNHANDLED\_ERRORS?

**URL:** <https://discuss.ray.io/t/how-to-suppress-taskcancellederror-without-ray-ignore-unhandled-errors/9104>\
**Category:** Ray Core\
**Created:** [January 25, 2023, 8:17pm UTC](https://discuss.ray.io/t/how-to-suppress-taskcancellederror-without-ray-ignore-unhandled-errors/9104 "2023-01-25T20:17:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![xcharleslin](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/xcharleslin/32/3781_2.png) [@xcharleslin](https://discuss.ray.io/u/xcharleslin)\
**Post date:** [January 25, 2023, 8:17pm UTC](https://discuss.ray.io/t/how-to-suppress-taskcancellederror-without-ray-ignore-unhandled-errors/9104/1 "2023-01-25T20:17:53Z")

</div>

**How severe does this issue affect your experience of using Ray?**

- Medium: It contributes to significant difficulty to complete my task, but I can work around it.

When I call `ray.cancel(r)` on an inflight object ref, the worker prints an exception, even if I don’t call ray.get().

```auto
ERROR worker.py:400 -- Unhandled error (suppress with 'RAY_IGNORE_UNHANDLED_ERRORS=1'): ray::fgh() (pid=13251, ip=192.168.1.15)
  File "<stdin>", line 7, in fgh
ray.exceptions.TaskCancelledError: Task: TaskID(62ffec03f52574bdffffffffffffffffffffffff03000000) was cancelled

```

I’d like to suppress this because my workload typically cancels a significant amount of tasks as a performance optimization. However, I don’t want to set ‘RAY\_IGNORE\_UNHANDLED\_ERRORS=1’ because I don’t want to suppress actual unexpected worker errors, like workers dying.

Is there a way to suppress just TaskCancelledError? I did try catching KeyboardInterrupt and other exceptions within the task but that did not seem to work.

---

<div class="post-metadata">

**Author:** ![rickyyx](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/rickyyx/32/2671_2.png) [@rickyyx](https://discuss.ray.io/u/rickyyx)\
**Post date:** [January 26, 2023, 7:21pm UTC](https://discuss.ray.io/t/how-to-suppress-taskcancellederror-without-ray-ignore-unhandled-errors/9104/2 "2023-01-26T19:21:52Z")

</div>

Hey @xcharleslin!

Do you have a small repro for this? I tried the below code but it doesn’t raise the canceled error if I don’t try to ray.get on it. (I used ray 2.2)

```auto
import ray

@ray.remote
def f(t):
    import time
    time.sleep(t)
    return t

obj2 = f.remote(9999)
ray.cancel(obj2)

obj = f.remote(3)
print(ray.get(obj))
print(ray.get(obj2))

```

Or if there is something I am missing here from your workflow?

---

<div class="post-metadata">

**Author:** ![xcharleslin](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/xcharleslin/32/3781_2.png) [@xcharleslin](https://discuss.ray.io/u/xcharleslin)\
**Post date:** [February 3, 2023, 12:03am UTC](https://discuss.ray.io/t/how-to-suppress-taskcancellederror-without-ray-ignore-unhandled-errors/9104/3 "2023-02-03T00:03:42Z")

</div>

hey Ricky, try this:

```python
import ray; import time; ray.init()

@ray.remote
def f():
    while True:
        print("f() running")
        time.sleep(1)

ref = f.remote()
time.sleep(2)
ray.cancel(ref)

```

Output:

```python
(f pid=146818) f() running
(f pid=146818) f() running
# pauses for a few seconds here
2023-02-02 16:00:32,819 ERROR worker.py:400 -- Unhandled error (suppress with 'RAY_IGNORE_UNHANDLED_ERRORS=1'): ray::f() (pid=146818, ip=192.168.86.60)
  File "<stdin>", line 6, in f
ray.exceptions.TaskCancelledError: Task: TaskID(2f709301c413ada5ffffffffffffffffffffffff11000000) was cancelled

```

---

<div class="post-metadata">

**Author:** ![rickyyx](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/rickyyx/32/2671_2.png) [@rickyyx](https://discuss.ray.io/u/rickyyx)\
**Post date:** [February 9, 2023, 10:01pm UTC](https://discuss.ray.io/t/how-to-suppress-taskcancellederror-without-ray-ignore-unhandled-errors/9104/4 "2023-02-09T22:01:02Z")

</div>

> [@xcharleslin](#):
>
> ```auto
> import ray; import time; ray.init()
> 
> @ray.remote
> def f():
> while True:
> print("f() running")
> time.sleep(1)
> 
> ref = f.remote()
> time.sleep(2)
> 
> ```

Hmmm I wasn’t able to reproduce it. Which ray version you are using? It might have been fixed in the nightly.
