Skip to content

exceptions raised before calling task_status.started loses cause & context #3261

Closed

Description

@jakkdl

This snippet eats cause&context

trio/src/trio/_core/_run.py

Lines 1387 to 1389 in 9c909fa

except BaseExceptionGroup as exc:
if len(exc.exceptions) == 1:
raise exc.exceptions[0] from None

a la https://flake8-async.readthedocs.io/en/latest/rules.html#async123

we currently only run the ruff reimplementation of flake8-async, which hasn't added that rule. So we should maybe consider running flake8-async as well, disabling any rules that ruff handles [better], or are noisy.

It should make use of trio._util.raise_saving_context.

repro:

import trio


async def task(task_status: trio.TaskStatus) -> None:
    e = ValueError("foo")
    e.__cause__ = SyntaxError("bar")
    e.__context__ = TypeError("baz")
    raise e

async def main() -> None:
    async with trio.open_nursery() as nursery:
        await nursery.start(task)

try:
    trio.run(main)
except ExceptionGroup as excgroup:
    # should be SyntaxError, is None
    print(excgroup.exceptions[0].__cause__)
    # should be TypeError, is the exceptiongroup
    print(repr(excgroup.exceptions[0].__context__))

Activity

  1. A5rocks commented on May 22, 2025

    @A5rocks
    Contributor

    I was looking at flake8-async:

    • we would need to bring in flake8 if we want to ignore the test directories?
    • I think the tags are out of sync -- would pre-commit really work?

    If those are bugs I can go make issues. (also generally theres a lot of errors raised...)

  2. jakkdl commented on May 22, 2025

    @jakkdl
    MemberAuthor

    we would need to bring in flake8 if we want to ignore the test directories?

    we can use the pre-commit files or exclude directives to exclude them

    I think the tags are out of sync -- would pre-commit really work?

    if that is a problem I would love to know so I can fix it!

  3. A5rocks commented on May 23, 2025

    @A5rocks
    Contributor

    we can use the pre-commit files or exclude directives to exclude them

    I was hoping to maybe be able to run it as a command, but this makes enough sense to me.

    if that is a problem I would love to know so I can fix it!

    Yeah for instance the tag referenced on the docs doesn't exist:

    (.venv) PS C:\Users\A5rocks\Documents\trio> pre-commit run -a
    [INFO] Initializing environment for https://github.com/python-trio/flake8-async.
    An unexpected error has occurred: CalledProcessError: command: ('C:\\Program Files\\Git\\cmd\\git.EXE', 'checkout', '25.5.2')
    return code: 1
    stdout: (none)
    stderr:
        error: pathspec '25.5.2' did not match any file(s) known to git
    Check the log at C:\Users\A5rocks\.cache\pre-commit\pre-commit.log
    

    This isn't really a problem (we can just use old versions in pre-commit, or maybe even commit hashes?) but it's suboptimal.

  4. jakkdl commented on May 23, 2025

    @jakkdl
    MemberAuthor

    whelp, it appears I broke git tagging in python-trio/flake8-async#321
    thank you, will fix!

    EDIT: fixing in python-trio/flake8-async#385

  5. added 2 commits that reference this issue on Feb 17, 2026
    51e3166
    8204de2
  6. added a commit that references this issue on Feb 18, 2026
    2fd138e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions