8000 More traceback cleaning · Issue #3279 · python-trio/trio · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

More traceback cleaning #3279

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
jakkdl opened this issue Jun 5, 2025 · 4 comments
Open

More traceback cleaning #3279

jakkdl opened this issue Jun 5, 2025 · 4 comments

Comments

@jakkdl
Copy link
Member
jakkdl commented Jun 5, 2025

https://vorpus.org/blog/beautiful-tracebacks-in-trio-v070/ made tracebacks in some scenarios nicer, but afaict it mostly just affects calls to trio.run and when spawning tasks. (Implementation. Tested in test_traceback_frame_removal).

But there's still a lot of other scenarios that cause verbose outputs:

import trio
import trio.testing

cl = trio.CapacityLimiter(1)
async def borrower() -> None:
    await cl.acquire()
    await trio.sleep_forever()

async def main() -> None:
    async with trio.open_nursery() as nursery:
        nursery.start_soon(borrower)
        await trio.testing.wait_all_tasks_blocked()

        with trio.fail_after(1):
            async with cl:
                pass
trio.run(main)
  + Exception Group Traceback (most recent call last):
  |   File "/home/h/Git/trio/clean_tb/test_foo.py", line 16, in <module>
  |     trio.run(main)
  |     ~~~~~~~~^^^^^^
  |   File "/home/h/Git/trio/clean_tb/src/trio/_core/_run.py", line 2529, in run
  |     raise runner.main_task_outcome.error
  |   File "/home/h/Git/trio/clean_tb/test_foo.py", line 10, in main
  |     async with trio.open_nursery() as nursery:
  |                ~~~~~~~~~~~~~~~~~^^
  |   File "/home/h/Git/trio/clean_tb/src/trio/_core/_run.py", line 1122, in __aexit__
  |     raise combined_error_from_nursery
  | ExceptionGroup: Exceptions from Trio nursery (1 sub-exception)
  +-+---------------- 1 ----------------
    | Traceback (most recent call last):
    |   File "/home/h/Git/trio/clean_tb/src/trio/_sync.py", line 342, in acquire_on_behalf_of
    |     self.acquire_on_behalf_of_nowait(borrower)
    |     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^
    |   File "/home/h/Git/trio/clean_tb/src/trio/_sync.py", line 312, in acquire_on_behalf_of_nowait
    |     raise trio.WouldBlock
    | trio.WouldBlock
    | 
    | During handling of the above exception, another exception occurred:
    | 
    | Traceback (most recent call last):
    |   File "/home/h/Git/trio/clean_tb/src/trio/_timeouts.py", line 185, in fail_after
    |     yield scope
    |   File "/home/h/Git/trio/clean_tb/test_foo.py", line 14, in main
    |     async with cl:
    |                ^^
    |   File "/home/h/Git/trio/clean_tb/src/trio/_sync.py", line 127, in __aenter__
    |     await self.acquire()
    |   File "/home/h/Git/trio/clean_tb/src/trio/_sync.py", line 323, in acquire
    |     await self.acquire_on_behalf_of(trio.lowlevel.current_task())
    |   File "/home/h/Git/trio/clean_tb/src/trio/_sync.py", line 347, in acquire_on_behalf_of
    |     await self._lot.park()
    |   File "/home/h/Git/trio/clean_tb/src/trio/_core/_parking_lot.py", line 190, in park
    |     await _core.wait_task_rescheduled(abort_fn)
    |   File "/home/h/Git/trio/clean_tb/src/trio/_core/_traps.py", line 208, in wait_task_rescheduled
    |     return (await _async_yield(WaitTaskRescheduled(abort_func))).unwrap()
    |            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^
    |   File "/home/h/Git/trio/clean_tb/.venv/lib/python3.13/site-packages/outcome/_impl.py", line 213, in unwrap
    |     raise captured_error
    |   File "/home/h/Git/trio/clean_tb/src/trio/_core/_run.py", line 1657, in raise_cancel
    |     raise Cancelled._create(
    |     ...<3 lines>...
    |     )
    | trio.Cancelled: cancelled due to deadline
    | 
    | During handling of the above exception, another exception occurred:
    | 
    | Traceback (most recent call last):
    |   File "/home/h/Git/trio/clean_tb/test_foo.py", line 13, in main
    |     with trio.fail_after(1):
    |          ~~~~~~~~~~~~~~~^^^
    |   File "/usr/lib/python3.13/contextlib.py", line 162, in __exit__
    |     self.gen.throw(value)
    |     ~~~~~~~~~~~~~~^^^^^^^
    |   File "/home/h/Git/trio/clean_tb/src/trio/_timeouts.py", line 187, in fail_after
    |     raise TooSlowError
    | trio.TooSlowError
    +------------------------------------

I'm not entirely sure what the best approach is to try and clean this up. We could try cleaning up tracebacks in Cancelled.__init__, or a custom [Base]ExceptionGroup that filters the traceback in child exceptions. Other options include modifying sys.excepthook, sprinkling tons of __traceback_hide__, or if there's a few repeat offenders rewriting select code to add fewer frames to the stack.

Would also be great to collect more examples of unhelpfully long tracebacks to see if there are repeat offenders that would give disproportionate impact to improve. @Zac-HD

@A5rocks
Copy link
Contributor
A5rocks commented Jun 6, 2025

Here's something closer to my platonic ideal:

  + Exception Group Traceback (most recent call last):
  |   File "/home/h/Git/trio/clean_tb/test_foo.py", line 16, in <module>
  |     trio.run(main)
  |     ~~~~~~~~^^^^^^
  |   File "/home/h/Git/trio/clean_tb/src/trio/_core/_run.py", line 2529, in run
  |     raise runner.main_task_outcome.error
  |   File "/home/h/Git/trio/clean_tb/test_foo.py", line 10, in main
  |     async with trio.open_nursery() as nursery:
  |                ~~~~~~~~~~~~~~~~~^^
  |   File "/home/h/Git/trio/clean_tb/src/trio/_core/_run.py", line 1122, in __aexit__
  |     raise combined_error_from_nursery
  | ExceptionGroup: Exceptions from Trio nursery (1 sub-exception)
  +-+---------------- 1 ----------------
# trimmed
    | Traceback (most recent call last):
    |   File "/home/h/Git/trio/clean_tb/src/trio/_timeouts.py", line 185, in fail_after
    |     yield scope
    |   File "/home/h/Git/trio/clean_tb/test_foo.py", line 14, in main
    |     async with cl:
    |                ^^
    |   File "/home/h/Git/trio/clean_tb/src/trio/_sync.py", line 127, in __aenter__
    |     await self.acquire()
    |   File "/home/h/Git/trio/clean_tb/src/trio/_sync.py", line 323, in acquire
    |     await self.acquire_on_behalf_of(trio.lowlevel.current_task())
# trimmed
    |   File "/home/h/Git/trio/clean_tb/src/trio/_core/_run.py", line 1657, in raise_cancel
    |     raise Cancelled._create(
    |     ...<3 lines>...
    |     )
    | trio.Cancelled: cancelled due to deadline
    | 
    | During handling of the above exception, another exception occurred:
    | 
    | Traceback (most recent call last):
    |   File "/home/h/Git/trio/clean_tb/test_foo.py", line 13, in main
    |     with trio.fail_after(1):
    |          ~~~~~~~~~~~~~~~^^^
# trimmed
    |   File "/home/h/Git/trio/clean_tb/src/trio/_timeouts.py", line 187, in fail_after
    |     raise TooSlowError
    | trio.TooSlowError
    +------------------------------------

I'm a bit surprised there's nothing pointing to trio.fail_after(1) in the Cancelled exception.

@A5rocks
Copy link
Contributor
A5rocks commented Jun 6, 2025

Actually part of this is just #2649 I think

@jakkdl
Copy link
Member Author
jakkdl commented Jun 10, 2025

ooh nice. Are there any blockers for #3233?

@A5rocks
Copy link
Contributor
A5rocks commented Jun 10, 2025

Well I missed that you actually approved it! I'll respond to your review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants
0