[New-bugs-announce] [issue46829] Confusing CancelError message if multiple cancellations are scheduled
report at bugs.python.org
Tue Feb 22 17:02:51 EST 2022
New submission from Andrew Svetlov <andrew.svetlov at gmail.com>:
Suppose multiple `task.cancel(msg)` with different messages are called on the same event loop iteration.
What message (`cancel_exc.args`) 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 is args[-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 / exc.args 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.
nosy: ajoino, alex.gronholm, asvetlov, chris.jerdonek, dreamsorcerer, gvanrossum, iritkatriel, jab, njs, tinchester, yselivanov
title: Confusing CancelError message if multiple cancellations are scheduled
versions: Python 3.11
Python tracker <report at bugs.python.org>
More information about the New-bugs-announce