Skip to content

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

@njsmith

Description

@njsmith

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.

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