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:100850] [Ruby master Feature#17325] Adds Fiber#cancel, which forces a Fiber to break/return
Date: Sat, 14 Nov 2020 22:43:57 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-88491.20201114224356.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).


This is my first big foray into the ruby C API, so I hope it makes sense what I was trying to do. Did I handle `vm_fiber_break` and `fiber_start` appropriately? Is there is somewhere more appropriate to save the cancel reason than a new `VALUE` on `rb_fiber_struct`? E.g. could I safely assign the canceled fiber's `ec->errinfo` from the calling Fiber, instead of using `cancel_reason`?

When this compiles, it warns about:
```
vm_insnhelper.h:251:1: warning: ‘vm_call_iseq_optimizable_p’ defined but not used [-Wunused-function]
  251 | vm_call_iseq_optimizable_p(const struct rb_callinfo *ci, const struct rb_callcache *cc)     
```
So I'm probably doing something wrong there, too.

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

* Author: nevans (Nicholas Evans)
* Status: Open
* Priority: Normal
----------------------------------------
This skips rescue and catch blocks but runs all ensure blocks. It can be run on any living fiber, and propagates cancellation to child (resumed) fibers.

Basically, I want it to behave as if you called `break` or `return` to jump all the way to the top frame of the fiber from wherever it is.. Exceptions might be too heavy-weight, can't (and shouldn't) be sent to resuming fibers, and can be caught. This can also be emulated via `catch/throw` with an anonymous `Object.new`, but that adds an extra stack frame, and implementing it reliably requires interception of `Fiber.yield`, e.g. adding a `Fiber.throw` (analogous to `Fiber.raise`) which also shouldn't be sent to resuming fibers.

I tried to match the new _(much improved!)_ transfer/resume switch semantics with cancel propagation. When the cancelation 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.

I think it's important to allow future `resume` and `transfer` out of a canceled fiber, in case it's necessary to close resources from `ensure` blocks. However, may have chosen the wrong behavior for multiple `cancels` on a `canceling`. I chose to use the same propagate/resume/transfer semantics but _not `break` again_ after subsequent cancels. On second thought, I think it should behave more like a standard `break`, and do exactly the same thing that `break` or `return` would normally do if they are called from an `ensure` block. I'll update this patch with that behavior when I'm able to.

---Files--------------------------------
0001-Added-Fiber-cancel-which-forces-a-Fiber-to-return.patch (14.4 KB)


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

  reply	other threads:[~2020-11-14 22:44 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 ` nicholas.evans [this message]
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 ` [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-88491.20201114224356.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).