Message413749
Suppose multiple `task.cancel(msg)` with different messages are called on the same event loop iteration.
What message (`cancel_exc.args[0]`) should be sent on the next loop iteration?
As of Python 3.10 it is the message from the *last* `task.cancel(msg)` call.
The main branch changed it to the *first* message (it is a subject for discussion still).
Both choices are equally bad. The order of tasks execution at the same loop iteration is weak and depends on very many circumstances. Effectively, first-cancelled-message and last-cancelled-message are equal to random-message.
This makes use of cancellation message not robust: a task can be cancelled by many sources, task-groups adds even more mess.
Guido van Rossum suggested that messages should be collected in a list and raised altogether. There is a possibility to do it in a backward-compatible way: construct the exception as `CancelledError(last_msg, tuple(msg_list))`. args[0] is args[1][-1]. Weird but works.
`.cancel()` should add `None` to the list of cancelled messages.
The message list should be cleared when a new CancelledError is constructed and thrown into cancelling task.
Working with exc.args[0] / exc.args[1] is tedious and error-prone. I propose adding `exc.msgs` property. Not sure if the last added message is worth a separate attribute, a robust code should not rely on messages order as I described above.
The single message is not very useful but a list of messages can be used in timeouts implementation as cancellation count alternative.
I don't have a strong preference now but want to carefully discuss possible opportunities before making the final decision. |
|
Date |
User |
Action |
Args |
2022-02-22 22:02:51 | asvetlov | set | recipients:
+ asvetlov, gvanrossum, njs, jab, alex.gronholm, chris.jerdonek, yselivanov, tinchester, iritkatriel, dreamsorcerer, ajoino |
2022-02-22 22:02:51 | asvetlov | set | messageid: <1645567371.24.0.086924022035.issue46829@roundup.psfhosted.org> |
2022-02-22 22:02:51 | asvetlov | link | issue46829 messages |
2022-02-22 22:02:50 | asvetlov | create | |
|