python-trio / python-trio/trio

collapse_exception_group breaks with exceptions that are also frozen dataclasses

Open
#2,607 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Quick preface:

  • this only happens with strict_exception_groups=False, and enabling strict exception groups fixes the "problem"
  • I'm not even sure I would necessarily consider this a "problem" with the code per se, but it should probably be at least documented somewhere
  • I realize that strict_exception_groups=False is on the deprecation march, so even if this is considered a problem, it might not be worthwhile to fix it
  • feel free to immediately close as wontfix!

That being said, in the interests of "more information is always better than less information" and "something something posterity", I thought I'd throw up a quick flag: collapse_exception_group breaks if the exception it's trying to collapse also happens to be a frozen dataclass. When trio tries to set the __traceback__ attribute, dataclass' __setattr__ magic kicks in, and you get a FrozenInstanceError. Also, since this happens in the middle of nursery exception management, it kinda... blows everything up.

Again, for me, the solution was suuuuper simple -- just enable strict exception groups and update the rest of my code to match. Voila, and I get futureproofing free of charge! But it wasn't immediately clear to me that that would be a viable solution, until I poked around some inside the trio codebase. So it might be helpful to document that somewhere (I have no idea where, or I'd just have directly opened up a PR).

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 collapse_exception_group in trio/_core/_run.py around line 147, then trace the strict_exception_groups=False behavior described in the issue. Document that frozen dataclass exceptions can fail during traceback assignment and that enabling strict exception groups avoids the problem; done means the limitation and workaround are easy to find.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.