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

[Question] How to stop a fiber? #263

Open
jsaak opened this issue Jul 26, 2023 · 4 comments
Open

[Question] How to stop a fiber? #263

jsaak opened this issue Jul 26, 2023 · 4 comments

Comments

@jsaak
Copy link

jsaak commented Jul 26, 2023

I have still no idea how to stop a Fiber from running (unschedule).
I was told that i can raise an exception, but it is not working as i hoped.
Can you help me how to do that?

In this code i was expecting @fib1 to exit, but it does not exit ever.

Do I have any alternative options?

require 'async'

Fiber.set_scheduler(Async::Scheduler.new)

@fib1 = Fiber.schedule do
  puts 'entering fiber 1'
  sleep 1
  @fib2.raise
  puts 'exiting fiber 1'
end

@fib2 = Fiber.schedule do
  puts 'entering fiber 2'
  begin
    sleep 4
  rescue
    puts 'got exception'
  end
  puts 'exiting fiber 2'
end
@jsaak
Copy link
Author

jsaak commented Jul 26, 2023

reading the source of Task, i found this:

Fiber.scheduler.raise(@fib2, "Stop")

And it seems to be working fine.
Any reason Fiber.raise works different?

@ioquatix
Copy link
Member

The problem with Fiber.raise is that it doesn't know how to schedule the original fiber that called it and it does an implicit transfer.

We should probably fix this in Ruby itself, so that it just works as expected.

Let me look into this issue in more detail this week.

@jsaak
Copy link
Author

jsaak commented Jul 26, 2023

Thanks!
I am happy that you think it is a bug too.
In the meantime i will use Fiber.scheduler.raise() .

(My program is a bit more complicated than the one i posted, i lost some hair figuring this out :)

@ioquatix
Copy link
Member

If you check Fiber.scheduler.raise you will see it handles the scheduling correctly.

@jsaak jsaak closed this as completed Aug 9, 2023
@jsaak jsaak reopened this Aug 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants