Rust Async Working Group
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Write
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
      • Invitee
    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Sharing URL Help
Menu
Options
Versions and GitHub Sync Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Write
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
Invitee
Publish Note

Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

Your note will be visible on your profile and discoverable by anyone.
Your note is now live.
This note is visible on your profile and discoverable online.
Everyone on the web can find and read all notes of this public team.
See published notes
Unpublish note
Please check the box to agree to the Community Guidelines.
View profile
Engagement control
Commenting
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
  • Everyone
Suggest edit
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
Emoji Reply
Enable
Import from Dropbox Google Drive Gist Clipboard
   owned this note    owned this note      
Published Linked with GitHub
Subscribed
  • Any changes
    Be notified of any changes
  • Mention me
    Be notified of mention me
  • Unsubscribe
Subscribe
--- title: "Reading notes: A case for CancellationTokens" tags: reading-club, WG-async date: 2023-09-21 url: https://hackmd.io/h27_RB3UTpK6V2ao3CntFg --- # Reading notes: A case for CancellationTokens Link to document: https://gist.github.com/Matthias247/354941ebcc4d2270d07ff0c6bf066c64 ## Async Drop as a method of graceful cancellation under linear rules Yosh: Under the header [Graceful Cancellation Methods](https://gist.github.com/Matthias247/354941ebcc4d2270d07ff0c6bf066c64#graceful-cancellation-models) there is no mention of "async Drop" as a way of graceful cancellation. ~~In the absence of [true linear types](https://blog.yoshuawuyts.com/linearity-and-control/), every value _must_ be safe to drop - and thus cancel. Imo the goal really ought to be to call code in response to that.~~ _Edit:_ I didn't initially cath that this post assumed we'd be operating under linear rules. It's not stated explicitly in this post, but I assume they're working designing from the point of: ["types which cannot be dropped"](https://blog.yoshuawuyts.com/linearity-and-control) interpretation of linear types. Rather than the: ["types whose destructor is guaranteed to be called"](https://blog.yoshuawuyts.com/linear-types-one-pager) interpretation of linear types. `fn poll_cancel` feels like it gets closest to this, but I would have liked to see async Drop mentioned here too. Especially since this document appears to have been written after Sabrina Jewson's post on [Async Drop](https://sabrinajewson.org/blog/async-drop). I wrote an additional example on how async-Drop based graceful shutdown could be leveraged for HTTP servers [here](https://hackmd.io/KqASjHfBR6am3z5INtKCJA?view#Example). This would work even for the "drop is guaranteed to be called" interpretation of linear types, as explained by Sabrina in [Appendix A](https://sabrinajewson.org/blog/async-drop#completion-futures) of her post. Yosh: Async drop is probably superior to this. tmandry: This post was written back in 2021. Yosh: The linear types approach wouldn't have been on anyone's radar at this point. eholk: Even if you have async drop, you have to poll the drop functions from sync code. Async cancellation is potentially useful as an `async Drop` implementation mechanism, even if `async Drop` turns out to be the better surface level semantics. ## Cancellation feedback zmitchell: It's unclear to me from the document how a caller is supposed to know that the operation was successfully cancelled. Would that just be signaled by the return type of the `async` operation, or is that intended to be stored in the `CancellationToken` somehow? eholk: I've done some prototyping around similar designs to this one, and ways to get the status of cancellation tend to fall out somewhat naturally in the type system. The exact details depend on the design, but you're almost forced to give feedback or explicitly ignore it. tmandry: How does that compose? If you're selecting over multilpe I/O features, do all of them need this? It's not obvious in general to me how this should compose. zmitchell: There's the assertion that you don't need to rewrite code, but I'm not sure that's true, for that reason. Also if you write a third state to the `Poll` enum, everyone has to rewrite everything. eholk: You change how you desugar await. Then the cancellation bits are invisible. Any manual poll function does need to take this into account. It seems to compose well in that case. zmitchell: There's a lot of code that assumes that Pending means !Ready. eholk: Much of that would break in silent ways, which would be unfortunate. tmandry: Adding a third variant to Poll is probably not feasible. ## Callbacks break the linear control flow of `async`/`await` zmitchell: Under the [Evaluating the options - CancellationTokens](https://gist.github.com/Matthias247/354941ebcc4d2270d07ff0c6bf066c64#option-d-cancellationtokens) section it proposes allowing the library author to provide a callback that's executed as soon as cancellation is requested. This is a viable solution for immediately cancelling work that's already in progress, but it's a bit of an impedance mismatch for how most `async` code I've seen is written e.g. top-down, linear control flow. It's also harder to reason about. zmitchell: This is more of a callback style which is what async/await is trying to get away from. tmandry: That struck me as odd. You could have a future that resolves if your task is canceled and select over that. eholk: In the prototyping I'm doing, I did end up writing a callback like that, and it is terrible. If we end up writing that in real life, we've clearly failed. The idea is that the cancellation behavior is in the leaf futures. In real-life there hopefully wouldn't be much reason to have this callback. It feels a bit like Defer blocks. Yosh: This is a bit like creating an anonymous `impl Drop`. ## Cancellation is just one kind of signal TC: For a project at work, we considered `CancellationTokens`, but we found that in our application that cancellation was just *one* of the many kinds of *signals* that we often needed to send to an ongoing task. So we instead architected to better support sending these (hierarchically-delivered) signals and that also handled cancellation. It's still unclear to me why cancellation would be special enough to deserve its own channel rather than just having a more standardized way to send arbitrary hierarchically-delivered asynchronous signals to asynchronous futures and tasks. TC: Even for cancellation, we needed to send different *flavors* of cancellation, e.g. whether the task should stop gracefully or immediately. tmandry: That's an interesting point. You want to get a signal back from the cancellation but it's going to depend what the signal is. ## Cancellation tokens are hard to manage Yosh: > The main benefit of CancellationTokens as shown is that they are extremely composable. Most code either does not have to be cancellation-aware (in case the token is implicitely forwarded), or does only have the responsibility to forward the token. Cancellation tokens somewhat famously don't compose well. In Golang they're passed through to every single function via an explicit `Context` object. And C# does something very similar. Bar the addition of a [context system to Rust](http://tmandry.gitlab.io/blog/posts/2021-12-21-context-capabilities/), you'll almost certainly run into issues when trying to pass cancellation tokens across trait boundaries. Async Rust's existing cancellation system does not have this issue. The only limitation it has is that we can't call asynchronous code in response to a cancellation - which some form of "async Drop" would ideally resolve. eholk: Since cancellation tokens would presumably be carried through the context, this is sort of hidden in Rust. Most of the examples in the post show cancellation tokens carried through explicit parameters to the `async fn`s though, so the composability problems are probably real. ## How different are these approaches actually? eholk: The various ways of expressing cancellation seem largely equivalent to each other. For example, if the cancellation token is passed through the context, then ```rust fn poll(self: Pin<&mut self>, cx: Context) { if cx.cancellation_token().is_cancelled() { // do cancellation } else { // normal poll } } ``` seems essentially equivalent to: ```rust fn poll(self: Pin<&mut Self>, cx: Context) { // normal poll } fn poll_cancel(self: Pin<&mut Self>, cx: Context) { // do cancellation } ``` You can do similar things with `request_cancellation` as well. tmandry: It was a bit unclear to me on the post whether it was about what the API should be vs what the mechanism or semantics should be. ## You don't need an explicit Cancellation Token type Yosh: In the [futures-time](https://docs.rs/futures-time/latest/futures_time/) crate we initially shipped a dedicated "CancellationToken" type. But we realized that there is no practical difference between a cancellation token and a zero-sized channel. This is probably a progression of TC's point above: if you want to use a remote channel rather than just dropping types, you probably want to be able to send different signals - not just the one. And channels support this out of the box. TC: +1. tmandry: You could choose what type to attach to the `Context` e.g. with a type parameter on `Future`, or the type provider API, ... ## Let's catch up on the linear types work TC: Yosh has been publishing a great series on his linear types work. As we talked about at the start of this meeting, the `CancellationToken` work far predated this. Let's be sure to schedule some time to read and talk through Yosh's work so that we're all caught up to the state of the art. tmandry, others: +1 (The meeting ended here.)

Import from clipboard

Paste your markdown or webpage here...

Advanced permission required

Your current role can only read. Ask the system administrator to acquire write and comment permission.

This team is disabled

Sorry, this team is disabled. You can't edit this note.

This note is locked

Sorry, only owner can edit this note.

Reach the limit

Sorry, you've reached the max length this note can be.
Please reduce the content or divide it to more notes, thank you!

Import from Gist

Import from Snippet

or

Export to Snippet

Are you sure?

Do you really want to delete this note?
All users will lose their connection.

Create a note from template

Create a note from template

Oops...
This template has been removed or transferred.
Upgrade
All
  • All
  • Team
No template.

Create a template

Upgrade

Delete template

Do you really want to delete this template?
Turn this template into a regular note and keep its content, versions, and comments.

This page need refresh

You have an incompatible client version.
Refresh to update.
New version available!
See releases notes here
Refresh to enjoy new features.
Your user state has changed.
Refresh to load new user state.

Sign in

Forgot password

or

By clicking below, you agree to our terms of service.

Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
Wallet ( )
Connect another wallet

New to HackMD? Sign up

Help

  • English
  • 中文
  • Français
  • Deutsch
  • 日本語
  • Español
  • Català
  • Ελληνικά
  • Português
  • italiano
  • Türkçe
  • Русский
  • Nederlands
  • hrvatski jezik
  • język polski
  • Українська
  • हिन्दी
  • svenska
  • Esperanto
  • dansk

Documents

Help & Tutorial

How to use Book mode

Slide Example

API Docs

Edit in VSCode

Install browser extension

Contacts

Feedback

Discord

Send us email

Resources

Releases

Pricing

Blog

Policy

Terms

Privacy

Cheatsheet

Syntax Example Reference
# Header Header 基本排版
- Unordered List
  • Unordered List
1. Ordered List
  1. Ordered List
- [ ] Todo List
  • Todo List
> Blockquote
Blockquote
**Bold font** Bold font
*Italics font* Italics font
~~Strikethrough~~ Strikethrough
19^th^ 19th
H~2~O H2O
++Inserted text++ Inserted text
==Marked text== Marked text
[link text](https:// "title") Link
![image alt](https:// "title") Image
`Code` Code 在筆記中貼入程式碼
```javascript
var i = 0;
```
var i = 0;
:smile: :smile: Emoji list
{%youtube youtube_id %} Externals
$L^aT_eX$ LaTeX
:::info
This is a alert area.
:::

This is a alert area.

Versions and GitHub Sync
Get Full History Access

  • Edit version name
  • Delete

revision author avatar     named on  

More Less

Note content is identical to the latest version.
Compare
    Choose a version
    No search result
    Version not found
Sign in to link this note to GitHub
Learn more
This note is not linked with GitHub
 

Feedback

Submission failed, please try again

Thanks for your support.

On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

Please give us some advice and help us improve HackMD.

 

Thanks for your feedback

Remove version name

Do you want to remove this version name and description?

Transfer ownership

Transfer to
    Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

      Link with GitHub

      Please authorize HackMD on GitHub
      • Please sign in to GitHub and install the HackMD app on your GitHub repo.
      • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
      Learn more  Sign in to GitHub

      Push the note to GitHub Push to GitHub Pull a file from GitHub

        Authorize again
       

      Choose which file to push to

      Select repo
      Refresh Authorize more repos
      Select branch
      Select file
      Select branch
      Choose version(s) to push
      • Save a new version and push
      • Choose from existing versions
      Include title and tags
      Available push count

      Pull from GitHub

       
      File from GitHub
      File from HackMD

      GitHub Link Settings

      File linked

      Linked by
      File path
      Last synced branch
      Available push count

      Danger Zone

      Unlink
      You will no longer receive notification when GitHub file changes after unlink.

      Syncing

      Push failed

      Push successfully