Skip to content
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

Disallow suppressing CancelledError #2835

Open
Dreamsorcerer opened this issue Feb 3, 2024 · 0 comments
Open

Disallow suppressing CancelledError #2835

Dreamsorcerer opened this issue Feb 3, 2024 · 0 comments
Labels
rule request Adding a new rule

Comments

@Dreamsorcerer
Copy link
Contributor

Dreamsorcerer commented Feb 3, 2024

Rule request

Disallow suppressing a CancelledError in most cases.

i.e.

try:
    ...
except asyncio.CancelledError:
    ...  # Code without an unconditional (re-)raise.

Reasoning

Suppressing cancellations is difficult to get right, and a developer should, at the very least, confirm they've understood the implications and different situations that a cancellation may occur.

A noqa comment can be used by the developer to confirm they've understood this. Alternatively, a couple of safe code patterns may be allowed without needing the noqa. Probably, if there is both a if task.done(): condition and a if asyncio.current_task().cancelling() check in the handler, then the warning is not needed (also, if there is an unconditional raise).

Documentation could link to https://superfastpython.com/asyncio-task-cancellation-best-practices/ for example (particularly, "Practice #1: Do Not Consume CancelledError", but there doesn't appear to be anchors to link to a section).

Possibly a separate rule could be to ensure that code which does suppress the exception also calls .uncancel():
https://superfastpython.com/asyncio-cancel-task-cancellation/

@Dreamsorcerer Dreamsorcerer added the rule request Adding a new rule label Feb 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rule request Adding a new rule
Projects
None yet
Development

No branches or pull requests

1 participant