python-trio / python-trio/trio
We need a mechanism to crash Trio with a given exception; how should it work?
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 7.3k
- Forks
- 431
- Avg merge
- 2d 17h
- Merged PRs (30d)
- 6
Description
There are a few places where we want to crash trio.run with a given exception. Meaning something like: cancel the main task, unwind everything, raise the given exception as the final result. This is needed for:
KeyboardInterrupt(when it's not delivered directly to a task) (#733, #1537, etc.)- Exceptions from "can't fail" functions like
run_sync_soonand system tasks (which also want a way to avoid raisingTrioInternalError– #1056) - When a deadlock is detected (#1085)
- When pytest-trio's test timeout expires (https://github.com/python-trio/pytest-trio/issues/53)
It's not quite clear what the semantics should be though!
One option: when asked to "crash", we:
- cancel the main task, if it's still running (if not, that means we're already shutting down, so we can skip this step)
- save the "crash" exception in the
Runnerstate somewhere - on the way out of
run, check if we have any saved exceptions, and if so, do... something with them. Ifmain_task_outcomeis aValue, it's easy, just raise aMultiError(saved_exceptions). If themain_task_outcomeis anError, I'm not sure what the best approach is.MultiError(saved_exceptions)with themain_task_outcomeas__context__? That could be confusing since the exception stored inmain_task_outcomemight have only occurred after and as a result of a sequence like "crash" → cancel main task → something on the cancel path raises an exception. I guess the only alternatives though are (a) discarding the main task exception, (b)MultiError(saved_exceptions + [main_task_exception]). Not sure if either of those is actually better.
Note that we're already doing something kinda similar at the end of unrolled_run to handle ki_pending.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reading the Runner state and the shutdown handling in run and unrolled_run, including the existing ki_pending logic. Trace how main_task_outcome and exceptions from system tasks are handled. A complete change would define the crash semantics for each listed case and cover the resulting exception behavior with tests.
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
- 20/100