python-trio / python-trio/trio

Cancelled AsyncResources eat exceptions

Open
#1,559 10 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

cancellation exception handling user happiness
Dominant language
Python
Stars
7.3k
Forks
431
Avg merge
2d 17h
Merged PRs (30d)
6

Description

This is an especially surprising special case of #455.

This code:

with trio.move_on_after(1):
    async with any_asyncresource:
        try:
            await trio.sleep_forever()
        finally:
            raise ValueError

will swallow the ValueError, because it gets stuck as the __context__ of the Cancelled exception raised in any_asyncresource.aclose. And aclose here is basically required to raise Cancelled, because Trio rules require it to be a checkpoint.

This showed up in #1555, with trio.Process standing in for "any_asyncresource".

This kind of thing keeps showing up for me and it's extremely confusing to track down. I know the party line is "who knows if someone was going to handle that exception that got stuck as the Cancelled __context__, we shouldn't reraise it when we swallow the Cancelled". But I think sometimes raising an exception that the user thought was handled is a much better failure mode than sometimes silently swallowing an exception that no one tried to handle at all.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the move_on_after example and inspect how cancellation, context, and asyncresource.aclose interact, including the trio.Process case from #1555. The issue names no file or test path; done requires an agreed behavior that avoids silently losing the ValueError while preserving the required cancellation checkpoint.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.