python-trio / python-trio/trio

Some way to take a cancellation in one call to wait_task_rescheduled and forward it to another task

Open
#642 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

In both #266 (shared tasks) and #606 (propagating cancellation across thread re-entry), we have a situation where we're pushing work from one place to another, so to Python (and the Trio core) it looks like two independent tasks, but logically it's one stack split over multiple tasks. Specifically:

  • One task schedules some work, and blocks in wait_task_rescheduled to wait for it to finish
  • Eventually, the work ends up being done in a system task, with exceptions etc. routing back to the original task
  • If the wait_task_rescheduled gets aborted, we want to propagate the cancellation to the system task, so that the cancellation actually works, and so that it gets raised at the appropriate place (the actual bottom of our stack), and ends up with a proper traceback

This is quite difficult currently.

What we want conceptually is like... to be able to open a cancel-scope-or-something-like-it in the system task, and then somehow "forward" cancellations from the original context to it. It's not immediately obvious to me how to do this with the current cancellation code – in particular there are subtleties like, if the parent context is cancelled normally, then we should put the child into a persistent cancelled state, and keep attempting to redeliver Cancelled with the parent's scope. If the parent has a KeyboardInterrupt delivered, we should try to forward on the raise_cancel logic to the child, so that if-and-only-if it accepts the exception, the pending_ki flag gets cleared.

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 by reading the cancellation code around wait_task_rescheduled and the raise_cancel logic mentioned in the issue. Trace how normal cancellation and KeyboardInterrupt are delivered between tasks, then define a design that forwards both cases with the stated persistent-cancellation and pending_ki semantics; done means the behavior is specified and covered by 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.