ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:117458] [Ruby master Bug#20414] `Fiber#raise` should recurse to `resumed_fiber` rather than failing.
@ 2024-04-07 14:17 ioquatix (Samuel Williams) via ruby-core
  2024-04-07 14:23 ` [ruby-core:117459] " ioquatix (Samuel Williams) via ruby-core
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: ioquatix (Samuel Williams) via ruby-core @ 2024-04-07 14:17 UTC (permalink / raw)
  To: ruby-core; +Cc: ioquatix (Samuel Williams)

Issue #20414 has been reported by ioquatix (Samuel Williams).

----------------------------------------
Bug #20414: `Fiber#raise` should recurse to `resumed_fiber` rather than failing.
https://bugs.ruby-lang.org/issues/20414

* Author: ioquatix (Samuel Williams)
* Status: Open
* Assignee: ioquatix (Samuel Williams)
* Backport: 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN
----------------------------------------
The following program will fail with `FiberError`, and is difficult to properly clean up:

```ruby
root_fiber = Fiber.current

f1 = Fiber.new do
	root_fiber.transfer
end

f2 = Fiber.new do
	f1.resume
end

f2.transfer

f2.raise("error") # => `raise': attempt to transfer to a resuming fiber (FiberError)
```

This program deliberately set's up a scenario where `f2` is resuming `f1`. Trying to raise an exception on `f2` is impossible, because the only way control flow can return to it, is when `f1` yields or exits.

We can avoid this problem, by raising the exception on f1, and we can do this automatically using the following logic:

```c
static VALUE
fiber_raise(rb_fiber_t *fiber, VALUE exception)
{
    // Add this recursive step:
    if (fiber->resuming_fiber) {
        return fiber_raise(fiber->resuming_fiber, exception);
    }

    // Existing code ...
    else if (FIBER_SUSPENDED_P(fiber) && !fiber->yielding) {
        return fiber_transfer_kw(fiber, -1, &exception, RB_NO_KEYWORDS);
    }
    else {
        return fiber_resume_kw(fiber, -1, &exception, RB_NO_KEYWORDS);
    }
}
```

This makes `Fiber#raise` much more robust and useful for the purpose of stopping fibers, without knowing exactly what they are doing.



-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [ruby-core:117459] [Ruby master Bug#20414] `Fiber#raise` should recurse to `resumed_fiber` rather than failing.
  2024-04-07 14:17 [ruby-core:117458] [Ruby master Bug#20414] `Fiber#raise` should recurse to `resumed_fiber` rather than failing ioquatix (Samuel Williams) via ruby-core
@ 2024-04-07 14:23 ` ioquatix (Samuel Williams) via ruby-core
  2024-04-07 14:26 ` [ruby-core:117461] " ioquatix (Samuel Williams) via ruby-core
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: ioquatix (Samuel Williams) via ruby-core @ 2024-04-07 14:23 UTC (permalink / raw)
  To: ruby-core; +Cc: ioquatix (Samuel Williams)

Issue #20414 has been updated by ioquatix (Samuel Williams).


Proposed change: https://github.com/ruby/ruby/pull/10482

----------------------------------------
Bug #20414: `Fiber#raise` should recurse to `resumed_fiber` rather than failing.
https://bugs.ruby-lang.org/issues/20414#change-107843

* Author: ioquatix (Samuel Williams)
* Status: Open
* Assignee: ioquatix (Samuel Williams)
* Backport: 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN
----------------------------------------
The following program will fail with `FiberError`, and is difficult to properly clean up:

```ruby
root_fiber = Fiber.current

f1 = Fiber.new do
	root_fiber.transfer
end

f2 = Fiber.new do
	f1.resume
end

f2.transfer

f2.raise("error") # => `raise': attempt to transfer to a resuming fiber (FiberError)
```

This program deliberately set's up a scenario where `f2` is resuming `f1`. Trying to raise an exception on `f2` is impossible, because the only way control flow can return to it, is when `f1` yields or exits.

We can avoid this problem, by raising the exception on f1, and we can do this automatically using the following logic:

```c
static VALUE
fiber_raise(rb_fiber_t *fiber, VALUE exception)
{
    // Add this recursive step:
    if (fiber->resuming_fiber) {
        return fiber_raise(fiber->resuming_fiber, exception);
    }

    // Existing code ...
    else if (FIBER_SUSPENDED_P(fiber) && !fiber->yielding) {
        return fiber_transfer_kw(fiber, -1, &exception, RB_NO_KEYWORDS);
    }
    else {
        return fiber_resume_kw(fiber, -1, &exception, RB_NO_KEYWORDS);
    }
}
```

This makes `Fiber#raise` much more robust and useful for the purpose of stopping fibers, without knowing exactly what they are doing.



-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [ruby-core:117461] [Ruby master Bug#20414] `Fiber#raise` should recurse to `resumed_fiber` rather than failing.
  2024-04-07 14:17 [ruby-core:117458] [Ruby master Bug#20414] `Fiber#raise` should recurse to `resumed_fiber` rather than failing ioquatix (Samuel Williams) via ruby-core
  2024-04-07 14:23 ` [ruby-core:117459] " ioquatix (Samuel Williams) via ruby-core
@ 2024-04-07 14:26 ` ioquatix (Samuel Williams) via ruby-core
  2024-04-17  6:20 ` [ruby-core:117548] " matz (Yukihiro Matsumoto) via ruby-core
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: ioquatix (Samuel Williams) via ruby-core @ 2024-04-07 14:26 UTC (permalink / raw)
  To: ruby-core; +Cc: ioquatix (Samuel Williams)

Issue #20414 has been updated by ioquatix (Samuel Williams).


With the proposed change, the following program:

```ruby
root_fiber = Fiber.current

f1 = Fiber.new do
  puts "f1 enter"
  root_fiber.transfer
  puts "f1 exit"
ensure
  puts "f1 ensure"
end

f2 = Fiber.new do
  puts "f2 enter"
  f1.resume
  puts "f2 exit"
ensure
  puts "f2 ensure"
end

f2.transfer

begin
  puts "raise"
  f2.raise(Interrupt)
rescue Interrupt => e
  puts "rescue Interrupt"
end
```

... generates the following output:

```
f2 enter
f1 enter
raise
f1 ensure
f2 ensure
rescue Interrupt
```

----------------------------------------
Bug #20414: `Fiber#raise` should recurse to `resumed_fiber` rather than failing.
https://bugs.ruby-lang.org/issues/20414#change-107848

* Author: ioquatix (Samuel Williams)
* Status: Open
* Assignee: ioquatix (Samuel Williams)
* Backport: 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN
----------------------------------------
The following program will fail with `FiberError`, and is difficult to properly clean up:

```ruby
root_fiber = Fiber.current

f1 = Fiber.new do
  root_fiber.transfer
end

f2 = Fiber.new do
  f1.resume
end

f2.transfer

f2.raise("error") # => `raise': attempt to transfer to a resuming fiber (FiberError)
```

This program deliberately set's up a scenario where `f2` is resuming `f1`. Trying to raise an exception on `f2` is impossible, because the only way control flow can return to it, is when `f1` yields or exits.

We can avoid this problem, by raising the exception on f1, and we can do this automatically using the following logic:

```c
static VALUE
fiber_raise(rb_fiber_t *fiber, VALUE exception)
{
    // Add this recursive step:
    if (fiber->resuming_fiber) {
        return fiber_raise(fiber->resuming_fiber, exception);
    }

    // Existing code ...
    else if (FIBER_SUSPENDED_P(fiber) && !fiber->yielding) {
        return fiber_transfer_kw(fiber, -1, &exception, RB_NO_KEYWORDS);
    }
    else {
        return fiber_resume_kw(fiber, -1, &exception, RB_NO_KEYWORDS);
    }
}
```

This makes `Fiber#raise` much more robust and useful for the purpose of stopping fibers, without knowing exactly what they are doing.



-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [ruby-core:117548] [Ruby master Bug#20414] `Fiber#raise` should recurse to `resumed_fiber` rather than failing.
  2024-04-07 14:17 [ruby-core:117458] [Ruby master Bug#20414] `Fiber#raise` should recurse to `resumed_fiber` rather than failing ioquatix (Samuel Williams) via ruby-core
  2024-04-07 14:23 ` [ruby-core:117459] " ioquatix (Samuel Williams) via ruby-core
  2024-04-07 14:26 ` [ruby-core:117461] " ioquatix (Samuel Williams) via ruby-core
@ 2024-04-17  6:20 ` matz (Yukihiro Matsumoto) via ruby-core
  2024-04-17 10:03 ` [ruby-core:117555] " ioquatix (Samuel Williams) via ruby-core
  2024-04-17 11:36 ` [ruby-core:117563] " ioquatix (Samuel Williams) via ruby-core
  4 siblings, 0 replies; 6+ messages in thread
From: matz (Yukihiro Matsumoto) via ruby-core @ 2024-04-17  6:20 UTC (permalink / raw)
  To: ruby-core; +Cc: matz (Yukihiro Matsumoto)

Issue #20414 has been updated by matz (Yukihiro Matsumoto).


I see no problem in the proposal. I agree.

Matz.


----------------------------------------
Bug #20414: `Fiber#raise` should recurse to `resumed_fiber` rather than failing.
https://bugs.ruby-lang.org/issues/20414#change-107942

* Author: ioquatix (Samuel Williams)
* Status: Open
* Assignee: ioquatix (Samuel Williams)
* Backport: 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN
----------------------------------------
The following program will fail with `FiberError`, and is difficult to properly clean up:

```ruby
root_fiber = Fiber.current

f1 = Fiber.new do
  root_fiber.transfer
end

f2 = Fiber.new do
  f1.resume
end

f2.transfer

f2.raise("error") # => `raise': attempt to transfer to a resuming fiber (FiberError)
```

This program deliberately set's up a scenario where `f2` is resuming `f1`. Trying to raise an exception on `f2` is impossible, because the only way control flow can return to it, is when `f1` yields or exits.

We can avoid this problem, by raising the exception on f1, and we can do this automatically using the following logic:

```c
static VALUE
fiber_raise(rb_fiber_t *fiber, VALUE exception)
{
    // Add this recursive step:
    if (fiber->resuming_fiber) {
        return fiber_raise(fiber->resuming_fiber, exception);
    }

    // Existing code ...
    else if (FIBER_SUSPENDED_P(fiber) && !fiber->yielding) {
        return fiber_transfer_kw(fiber, -1, &exception, RB_NO_KEYWORDS);
    }
    else {
        return fiber_resume_kw(fiber, -1, &exception, RB_NO_KEYWORDS);
    }
}
```

This makes `Fiber#raise` much more robust and useful for the purpose of stopping fibers, without knowing exactly what they are doing.



-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [ruby-core:117555] [Ruby master Bug#20414] `Fiber#raise` should recurse to `resumed_fiber` rather than failing.
  2024-04-07 14:17 [ruby-core:117458] [Ruby master Bug#20414] `Fiber#raise` should recurse to `resumed_fiber` rather than failing ioquatix (Samuel Williams) via ruby-core
                   ` (2 preceding siblings ...)
  2024-04-17  6:20 ` [ruby-core:117548] " matz (Yukihiro Matsumoto) via ruby-core
@ 2024-04-17 10:03 ` ioquatix (Samuel Williams) via ruby-core
  2024-04-17 11:36 ` [ruby-core:117563] " ioquatix (Samuel Williams) via ruby-core
  4 siblings, 0 replies; 6+ messages in thread
From: ioquatix (Samuel Williams) via ruby-core @ 2024-04-17 10:03 UTC (permalink / raw)
  To: ruby-core; +Cc: ioquatix (Samuel Williams)

Issue #20414 has been updated by ioquatix (Samuel Williams).


Thanks @matz.

To clarify, there are two issues are we addressing.

(1) Improve consistency of `Thread.current.raise` and `Fiber.current.raise`.
(2) Follow `resuming_fiber` when raising exceptions.

They overlap, so are addressed by the same PR.

----------------------------------------
Bug #20414: `Fiber#raise` should recurse to `resumed_fiber` rather than failing.
https://bugs.ruby-lang.org/issues/20414#change-107951

* Author: ioquatix (Samuel Williams)
* Status: Open
* Assignee: ioquatix (Samuel Williams)
* Backport: 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN
----------------------------------------
The following program will fail with `FiberError`, and is difficult to properly clean up:

```ruby
root_fiber = Fiber.current

f1 = Fiber.new do
  root_fiber.transfer
end

f2 = Fiber.new do
  f1.resume
end

f2.transfer

f2.raise("error") # => `raise': attempt to transfer to a resuming fiber (FiberError)
```

This program deliberately set's up a scenario where `f2` is resuming `f1`. Trying to raise an exception on `f2` is impossible, because the only way control flow can return to it, is when `f1` yields or exits.

We can avoid this problem, by raising the exception on f1, and we can do this automatically using the following logic:

```c
static VALUE
fiber_raise(rb_fiber_t *fiber, VALUE exception)
{
    // Add this recursive step:
    if (fiber->resuming_fiber) {
        return fiber_raise(fiber->resuming_fiber, exception);
    }

    // Existing code ...
    else if (FIBER_SUSPENDED_P(fiber) && !fiber->yielding) {
        return fiber_transfer_kw(fiber, -1, &exception, RB_NO_KEYWORDS);
    }
    else {
        return fiber_resume_kw(fiber, -1, &exception, RB_NO_KEYWORDS);
    }
}
```

This makes `Fiber#raise` much more robust and useful for the purpose of stopping fibers, without knowing exactly what they are doing.



-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [ruby-core:117563] [Ruby master Bug#20414] `Fiber#raise` should recurse to `resumed_fiber` rather than failing.
  2024-04-07 14:17 [ruby-core:117458] [Ruby master Bug#20414] `Fiber#raise` should recurse to `resumed_fiber` rather than failing ioquatix (Samuel Williams) via ruby-core
                   ` (3 preceding siblings ...)
  2024-04-17 10:03 ` [ruby-core:117555] " ioquatix (Samuel Williams) via ruby-core
@ 2024-04-17 11:36 ` ioquatix (Samuel Williams) via ruby-core
  4 siblings, 0 replies; 6+ messages in thread
From: ioquatix (Samuel Williams) via ruby-core @ 2024-04-17 11:36 UTC (permalink / raw)
  To: ruby-core; +Cc: ioquatix (Samuel Williams)

Issue #20414 has been updated by ioquatix (Samuel Williams).

Status changed from Open to Closed
Backport changed from 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN to 3.1: REQUIRED, 3.2: REQUIRED, 3.3: REQUIRED

Merged in <https://github.com/ruby/ruby/commit/6ade36c06b7cef948099b8f5f483763498705d12>.

----------------------------------------
Bug #20414: `Fiber#raise` should recurse to `resumed_fiber` rather than failing.
https://bugs.ruby-lang.org/issues/20414#change-107962

* Author: ioquatix (Samuel Williams)
* Status: Closed
* Assignee: ioquatix (Samuel Williams)
* Backport: 3.1: REQUIRED, 3.2: REQUIRED, 3.3: REQUIRED
----------------------------------------
The following program will fail with `FiberError`, and is difficult to properly clean up:

```ruby
root_fiber = Fiber.current

f1 = Fiber.new do
  root_fiber.transfer
end

f2 = Fiber.new do
  f1.resume
end

f2.transfer

f2.raise("error") # => `raise': attempt to transfer to a resuming fiber (FiberError)
```

This program deliberately set's up a scenario where `f2` is resuming `f1`. Trying to raise an exception on `f2` is impossible, because the only way control flow can return to it, is when `f1` yields or exits.

We can avoid this problem, by raising the exception on f1, and we can do this automatically using the following logic:

```c
static VALUE
fiber_raise(rb_fiber_t *fiber, VALUE exception)
{
    // Add this recursive step:
    if (fiber->resuming_fiber) {
        return fiber_raise(fiber->resuming_fiber, exception);
    }

    // Existing code ...
    else if (FIBER_SUSPENDED_P(fiber) && !fiber->yielding) {
        return fiber_transfer_kw(fiber, -1, &exception, RB_NO_KEYWORDS);
    }
    else {
        return fiber_resume_kw(fiber, -1, &exception, RB_NO_KEYWORDS);
    }
}
```

This makes `Fiber#raise` much more robust and useful for the purpose of stopping fibers, without knowing exactly what they are doing.



-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2024-04-17 11:36 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-04-07 14:17 [ruby-core:117458] [Ruby master Bug#20414] `Fiber#raise` should recurse to `resumed_fiber` rather than failing ioquatix (Samuel Williams) via ruby-core
2024-04-07 14:23 ` [ruby-core:117459] " ioquatix (Samuel Williams) via ruby-core
2024-04-07 14:26 ` [ruby-core:117461] " ioquatix (Samuel Williams) via ruby-core
2024-04-17  6:20 ` [ruby-core:117548] " matz (Yukihiro Matsumoto) via ruby-core
2024-04-17 10:03 ` [ruby-core:117555] " ioquatix (Samuel Williams) via ruby-core
2024-04-17 11:36 ` [ruby-core:117563] " ioquatix (Samuel Williams) via ruby-core

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).