Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve coroutine cancel semantics #1261

Closed
andrewrk opened this issue Jul 18, 2018 · 0 comments · Fixed by #1307
Closed

improve coroutine cancel semantics #1261

andrewrk opened this issue Jul 18, 2018 · 0 comments · Fixed by #1307
Labels
proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Milestone

Comments

@andrewrk
Copy link
Member

  • Rename cancel to cancelsuspend. It's only for canceling coroutines which have used the suspend keyword, and they must be in the suspend state, and not running (at least not accessing the coroutine frame).

  • Add cancelasync which is used for canceling an async function call. This is thread-safe and can be called at any time, even if the coroutine is running. It atomically sets a flag, and then any time the async function would start suspending - this includes doing an await - it instead performs a cancelsuspend on itself.

  • When a coroutine is destroyed via cancelsuspend (always at a suspend point), it runs the defer and errdefer expressions in scope, and then either does cancelsuspend on the awaiter, or if there is no awaiter yet, sets a flag which the awaiter will discover and perform a cancelsuspend on itself.

So the rule to remember is:

  • Every use of suspend has the responsibility to have a plan for how some code, somewhere, will either cancelsuspend or resume the handle.
  • Every use async has the responsibility to make sure that the resulting handle gets cancelasync or await applied to it.

Most user code will use async, await, and cancelasync, however there is occasionally the necessity to use suspend and cancelsuspend.

@andrewrk andrewrk added the proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. label Jul 18, 2018
@andrewrk andrewrk added this to the 0.3.0 milestone Jul 18, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant