Skip to content

raise_single_exception_from_group causes SystemExit to have no exit status #3275

Closed

Description

@seowalex

raise_single_exception_from_group was introduced as part of #3240 and #3197, but handles KeyboardInterrupt and SystemExit by wrapping the original exception in an exception of the same type:

# immediately bail out if there's any KI or SystemExit
for e in eg.exceptions:
    if isinstance(e, (KeyboardInterrupt, SystemExit)):
        raise type(e) from eg

Notably, this causes the resulting SystemExit to have no exit status, so system.exit(1) (or really any other exit status) does not properly set the exit status. I do not see a good reason why the exception should not be raised directly:

# immediately bail out if there's any KI or SystemExit
for e in eg.exceptions:
    if isinstance(e, (KeyboardInterrupt, SystemExit)):
        raise e from eg

Was there a reason for this handling?

Activity

  1. A5rocks commented on May 29, 2025

    @A5rocks
    Contributor

    I assume to ensure trackbacks in the exception group remain alright. But we can probably do some handling cause this is only for SystemExit and KeyboardInterrupt.

    cc @jakkdl

  2. jakkdl commented on May 30, 2025

    @jakkdl
    Member

    yeah it's got special handling to bailout ASAP instead of doing special logic, but it should indeed preserve e.args. Feel free to contribute a PR, should be a simple one :)

  3. seowalex commented on May 31, 2025

    @seowalex
    Author

    Sure, I can do that. I presume my suggested solution above works?

  4. CoolCat467 commented on May 31, 2025

    @CoolCat467
    Member

    I don't see any reason immediately why it wouldn't work, no.

  5. A5rocks commented on May 31, 2025

    @A5rocks
    Contributor

    raise e from eg might mutate the trace back of e in eg; just make sure it doesn't.

  6. jakkdl commented on Jun 2, 2025

    @jakkdl
    Member

    raise e from eg might mutate the trace back of e in eg; just make sure it doesn't.

    oh yeah, the helper is in large part specifically to avoid this problem. See python-trio/trio-websocket#191 or HypothesisWorks/hypothesis#4115.

    I think the better fix is to do something like

    # immediately bail out if there's any KI or SystemExit
    for e in eg.exceptions:
        if isinstance(e, (KeyboardInterrupt, SystemExit)):
            raise type(e)(e.args) from eg
  7. seowalex commented on Jun 6, 2025

    @seowalex
    Author

    I don't think e.args works for SystemExit, but I will look at the issues and work something out.

  8. added a commit that references this issue on Jun 6, 2025
    cdbbda9
  9. added a commit that references this issue on Jun 9, 2025
    be7faad
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions