This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author asvetlov
Recipients ajoino, alex.gronholm, asvetlov, chris.jerdonek, dreamsorcerer, gvanrossum, iritkatriel, jab, njs, tinchester, yselivanov
Date 2022-02-22.22:02:50
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1645567371.24.0.086924022035.issue46829@roundup.psfhosted.org>
In-reply-to
Content
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.
History
Date User Action Args
2022-02-22 22:02:51asvetlovsetrecipients: + asvetlov, gvanrossum, njs, jab, alex.gronholm, chris.jerdonek, yselivanov, tinchester, iritkatriel, dreamsorcerer, ajoino
2022-02-22 22:02:51asvetlovsetmessageid: <1645567371.24.0.086924022035.issue46829@roundup.psfhosted.org>
2022-02-22 22:02:51asvetlovlinkissue46829 messages
2022-02-22 22:02:50asvetlovcreate