-
Notifications
You must be signed in to change notification settings - Fork 232
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
Throw CancelledException into cancelled tasks #1392
base: fujitatomoya/check-task-canceled
Are you sure you want to change the base?
Throw CancelledException into cancelled tasks #1392
Conversation
I am by no means an expert, but the changes made to so far seem very logical to me. For your potential solutions to the executor problem:
task = executor.create_task(some_coro)
try:
await task
except SomeException:
....
if task.cancelled():
... instead of try:
await executor.create_task(some_coro)
except CancelledException:
...
except SomeOtherException:
.... As I wrote this I realized you need to do so anyways if you want to cancel the task, so not as much of an issue, but I'd prefer the expanded try except over another condition check task = executor.create_task(some_coro)
task.add_done_callback(done_cb)
task.cancel()
def done_cb(task):
try:
task.result()
except Exception:
.... Please correct me if I am wrong: As far as I understand all exceptions raised during the execution of the task are already set as the tasks exception: All we need to do is make sure the executor does not crash when the exceptions are raised in the first place? |
Let's say we're going with the asyncio logging approach, we have to decide on the logging behavior. As I see it, the two possible ways to retrieve exceptions from tasks is either awaiting the task or fetching the exception in a done_callback.
What do you think? Should we stick with the asyncio approach? |
PR #1377 addresses issue #1099 by filtering out cancelled tasks. The original attempt encountered issues where cancelled coroutines were not closed properly. This is resolved by throwing CancelledException into the coroutine, similar to the behavior of asyncio.
The solution is not ready yet, as the current implementation raises CancelledException when calling task.result(), which crashes the executor due to the way exceptions in tasks are currently treated:
rclpy/rclpy/rclpy/executors.py
Lines 853 to 855 in 4e8b071
Potential Solutions
task.result()
: PreventCancelledException
from being set inself._exception
. Users would need to calltask.cancelled()
to check cancellation status instead.CancelledException
: Allow the exception to be raised but modify the executor to prevent it from crashing.is_awaited
): The flag is set upon entering__await__
, causing the executor to ignore or log any exception.Would love to hear from you :)
@weber-niklas @fujitatomoya @haudren-woven @sloretz