python-trio / python-trio/trio

How should fail_at & fail_after handle MultiErrors?

Open
#59 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Suppose that something like MultiError([KeyError(), Cancelled()]) arrives at a with fail_after(...). Right now the Cancelled is absorbed, and then the KeyError continues propagating, because fail_after only raises TooSlowError if the block exits without any exception.

An alternative that might make more sense is to use a MultiError.catch to replace each Cancelled with a TooSlowError. The tricky bit is that right now, the abstraction boundaries don't really give fail_after a way to do this -- the MultiError.catch is hidden inside the cancel scope.

One option would be to move this functionality into open_cancel_scope itself. Another would be to make the Cancelled._scope attribute public, allowing an implementation like:

@contextmanager
def fail_at(deadline):
    with move_on_at(deadline) as scope:
        def handler(exc):
            if isinstance(exc, Cancelled) and exc.scope is scope:
                new_exc = TooSlowError()
                new_exc.__cause__ = exc
                return new_exc
            return exc
        with MultiError.catch(handler):
            yield scope

(The other advantage of this is that it would preserve the traceback of the Cancelled error, which currently gets lost. This is useful to see what operation froze.)

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 with fail_at, fail_after, open_cancel_scope, and MultiError.catch, then trace how Cancelled is absorbed and how scope ownership is represented. Resolve the intended handling for mixed MultiErrors, including whether Cancelled becomes TooSlowError and whether its traceback is preserved; add coverage for MultiError([KeyError(), Cancelled()]) once the behavior is agreed.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.