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

Issue #17325 has been updated by Eregon (Benoit Daloze).


This sounds like Thread#kill but for Fiber.
However, Fiber is cooperative so it seems this shouldn't be needed.

What's a concrete issue with Fiber#raise? It seems redundant with it to me.
If the application swallows Exception I think that's the application problem, and I think we should not add a second way to unwind a Fiber just for that.
The performance difference is likely not very relevant, as I expect exiting a Fiber is not a common operation, and most of the cost is likely unwinding the stack & running `ensure`.
Because this runs `ensure`, it's still possible for that Fiber to hang, or to raise another exception in `ensure` which can then be caught.
So, it's not a guarantee either.
`fiber.raise(Exception)` seems enough, and this seems to be only marginally different, at the cost of another method and yet another way to terminate a Fiber.

Regarding propagation, I'm not sure what's the difference, but exceptions in Fibers are propagated:
```
$ ruby -e 'f=Fiber.new { raise "foo" }; p((f.resume rescue $!))'
#<RuntimeError: foo>
```

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

* 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:
* 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:
* `catch` adds an extra stack frame.
* It would need to add `Fiber#throw` (or wrap/intercept `Fiber.yield`).
* A hypothetical `Fiber#throw` should probably only be allowed on yielding fibers (like `Fiber#resume`). (It wouldn't propagate.)

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



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

  parent reply	other threads:[~2020-11-15 22:59 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 ` eregontp [this message]
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 ` [ruby-core:100906] " nicholas.evans
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-88507.20201115225902.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).