Skip to content

Warn on finally: cancel_scope.cancel() #365

Description

@Zac-HD

Like async102 and async120, this is to address the concerns discussed in python-trio/trio#455. (also: should the rules docs link there?)

with CancelScope() as scope:
    try:
        ...  # whatever sync and async code here
    finally:
        scope.cancel()
        # You'd hope this just stops any ongoing work, but it swallows exceptions too!

In most cases the solution is simply to scope.cancel() without the try/finally block - structured concurrency means we reliably clean up on exceptions anyway. For some, the cancel scope is entirely unnecessary, and you could call e.g. recv_chan.close() instead of cancelling.

Metadata

Metadata

Assignees

No one assigned

    Labels

    new rulenew rule ideapostponedLow priority, blocked, or similar.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions