ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: nicholas.evans@gmail.com
To: ruby-core@ruby-lang.org
Subject: [ruby-core:100906] [Ruby master Feature#17325] Adds Fiber#cancel, which forces a Fiber to break/return
Date: Tue, 17 Nov 2020 15:45:14 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-88558.20201117154514.33540@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-17325.20201114224331.33540@ruby-lang.org

Issue #17325 has been updated by nevans (Nicholas Evans).

Description updated

one more update to alternatives

----------------------------------------
Feature #17325: Adds Fiber#cancel, which forces a Fiber to break/return
https://bugs.ruby-lang.org/issues/17325#change-88558

* Author: nevans (Nicholas Evans)
* Status: Open
* Priority: Normal
----------------------------------------

Calling `Fiber#cancel` will force a fiber to return, skipping rescue and catch blocks but running all ensure blocks. It behaves as if a `break` or `return` were used to jump from the last suspension point to the top frame of the fiber. Control will be transferred to the canceled fiber so it can run its ensure blocks.

## Propagation from resuming to resumed fibers

Any non-root living fiber can be canceled and cancellation will propagate to child (resumed) fibers. In this way, a suspended task can be canceled even if it is e.g. resuming into an enumerator, and the enumerator will be canceled as well. Transfer of control should match #17221's *(much improved)* transfer/resume semantics. After the cancellation propagates all the way to the bottom of the fiber resume stack, the last fiber in the chain will then be resumed. Resuming fibers will not run until they are yielded back into.

## Suspension of canceled fibers

Canceled fibers can still transfer control with `resume`, `yield`, and `transfer`, which may be necessary in order to release resources from `ensure` blocks. For simplicity, subsequent cancels will behave similarly to calling `break` or `return` inside an `ensure` block, and the last cancellation reason will overwrite earlier reasons.

## Alternatives

`Fiber#raise` could be used, but:
* Can only raise on resumable fibers.
* Cannot propagate cancellation down to resumed fibers.
* Exceptions are bigger and slower than `break`.
* `#raise` can't (and shouldn't) be sent to resuming fibers. (It can't propagate.)
* Exceptions can be caught. This might be desirable, but that should be at the discretion of the calling fiber.

Catch/Throw could be used (with an anonymous `Object.new`), but:
* We would need to add `Fiber#throw` (or wrap/intercept `Fiber.yield`).
* A hypothetical `Fiber#throw` should probably have similar semantics to `#resume` and thus only be allowed on resumable fibers.
  * In that case, it wouldn't propagate down to resumed fibers.
* `catch` adds an extra stack frame.

We could use go-style "Context" objects that contain a "done?" queue/future.
* These would need to be explicitly passed around.
* Although their usage could be enforced via linters like rubocop, I think that placing it off to the side will give developers the impression that it is optional Some sort of cancel propagation mechanism is not optional for structured concurrency.
* It should built into any task-scheduler library, which would allow application code to use it explicitly.
* But this suffers the same problem as current Fiber wrappers: it works fine if your code uses the wrapper, but code that uses fibers without the wrapper can be incompatible and introduce bugs (e.g. fibers that are released without running their `ensure` blocks).
* This make sense for a language like go which doesn't have exceptions but does have a convention of returning an "error" value. It feels out of place in ruby, IMO. Letting the fiber-task-scheduler mitigates that... for code that uses the fiber-task-scheduler.

We could add a keyword option to `Fiber#raise` that gives it similar propagation semantics to this.
* IMO, the simplicity of `Fiber#raise` simply being a specialized version of `Fiber#resume` is worth preserving.
* The propagation changes alone are enough of a semantic difference to warrant a new method.

We could implement `Fiber#cancel` by using `fiber.raise(FiberCancellationError)` on the bottom fiber and catching that exception during termination of the canceled fiber.
* This would have the "benefit" that the exception could be rescued.
* I might be wrong, but I think that doing this would mostly duplicate my PR, but with some added complexity around exception construction and catching.
* It might be a good keyword option? e.g. `Fiber#cancel(with_exception: [true,#exception,#to_str])`

Just let the task-fiber-scheduler library handle this.
* That's what I'm already doing now. It's mostly fine. It works in my code.
* Putting it into ruby core should lead to a small performance boost on very commonly repeated code.
  * There's probably a better way to store the `cancel_reason` that doesn't require the overhead of adding another `VALUE` to `rb_fiber_struct`. Maybe it can be placed directly into `errinfo`?
* Although the common cases can be handled via a trampoline fiber or #17221, there can still be situations where your application's fiber-scheduler library might not know about fibers created by other libraries. This adds interoperability to a common scenario.
* Coroutine cancellation is IMO a core feature. It's important to have something like this for all applications and libraries to use as a baseline for interoperability.

Implementation:  https://github.com/ruby/ruby/pull/3766



-- 
https://bugs.ruby-lang.org/

  parent reply	other threads:[~2020-11-17 15:45 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-14 22:43 [ruby-core:100849] [Ruby master Feature#17325] Adds Fiber#cancel, which forces a Fiber to break/return nicholas.evans
2020-11-14 22:43 ` [ruby-core:100850] " nicholas.evans
2020-11-14 22:45 ` [ruby-core:100851] " nicholas.evans
2020-11-15  5:35 ` [ruby-core:100854] " nicholas.evans
2020-11-15 20:48 ` [ruby-core:100857] " nicholas.evans
2020-11-15 22:59 ` [ruby-core:100863] " eregontp
2020-11-17 15:02 ` [ruby-core:100901] " nicholas.evans
2020-11-17 15:20 ` [ruby-core:100903] " nicholas.evans
2020-11-17 15:40 ` [ruby-core:100904] " nicholas.evans
2020-11-17 15:45 ` nicholas.evans [this message]
2020-11-17 16:25 ` [ruby-core:100910] " nicholas.evans
2020-11-17 18:37 ` [ruby-core:100914] " nicholas.evans
2020-11-19  0:34 ` [ruby-core:100941] " eregontp
2022-12-02 20:57 ` [ruby-core:111163] " nevans (Nicholas Evans)
2022-12-02 21:26 ` [ruby-core:111164] " ioquatix (Samuel Williams)
2022-12-03 15:05 ` [ruby-core:111177] " nevans (Nicholas Evans)
2023-05-22 11:19 ` [ruby-core:113591] " ioquatix (Samuel Williams) via ruby-core

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.ruby-lang.org/en/community/mailing-lists/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.journal-88558.20201117154514.33540@ruby-lang.org \
    --to=ruby-core@ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).