python-trio / python-trio/trio

Proposal: make checkpoint_if_cancelled() sync-colored and rename it accordingly

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

Nobody has claimed this yet.

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

Description

I'd like to propose that we deprecate checkpoint_if_cancelled() in favor of a synchronous function that I'm thinking of as trio.check_cancelled() (happy to take suggestions on the name, or move into hazmat). This would raise Cancelled if the current scope is cancelled, and do nothing otherwise. i.e., it is a pure cancel point, not a schedule point, not even a schedule point if we're cancelled.

This becomes possible due to some recent or pending changes:

  • #474, which permits the sync-color by saying we don't need to checkpoint before raising an exception (I'm assuming Cancelled counts as an exception for the purposes of that rule)
  • #901, which means the Cancelled-raising path is just raise Cancelled._init()
  • #958, which makes it fast to see if we're cancelled or not

And I think would have benefits for:

  • #70, by implementing efficient checkpoint() as await cancel_shielded_checkpoint(); check_cancelled()
  • #606, allowing run_sync_in_worker_thread functions to call check_cancelled() occasionally without messing with portals (because it's synchronous! and if/when #958 lands I think we can make the check_cancelled() operation sufficiently thread-safe)
  • iirc #658 indicated some user interest in a sync-colored raise-cancelled operation?

Possible downside: a sync-colored function called from an async function can now call check_cancelled() and wind up raising Cancelled. Previously that couldn't happen. My feeling is that "functions that do something off-the-wall might raise an exception their caller wasn't expecting" is hardly a new problem, but maybe there's some aspect of what we'd be giving up here that I'm missing.

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 proposal and the referenced issues #474, #901, #958, #70, #606, and #658, then review the five-comment thread. Determine whether checkpoint_if_cancelled() should be deprecated in favor of a synchronous check_cancelled() API, including its name, location, cancellation behavior, and thread-safety expectations.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend, distributed-systems
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
28/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.