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

Thread#join sometimes doesn't wake up when thread terminates #4

Open
ashtneoi opened this issue Jul 23, 2022 · 1 comment
Open

Thread#join sometimes doesn't wake up when thread terminates #4

ashtneoi opened this issue Jul 23, 2022 · 1 comment

Comments

@ashtneoi
Copy link

ashtneoi commented Jul 23, 2022

I'm using fiber_scheduler 0.13.0. Here's my code:

require 'fiber_scheduler'

Fiber.set_scheduler FiberScheduler.new
Fiber.schedule do
    t = Thread.new do
    end
    t.join 3
end

Sometimes t.join 3 returns quickly, which is what I want to happen since thread t almost immediately terminates. However, usually t.join 3 takes 3 seconds to return, which means something (the fiber scheduler, I think) prevented t.join from waking up early.

By adding debug logging to fiber_scheduler, I've figured out two things:

  • when t.join 3 returns quickly, it's because FiberScheduler::Selector#select called IO.select([], [], nil, 0)
  • when t.join 3 returns after 3 seconds, it's because FiberScheduler::Selector#select called IO.select([], [], nil, 3)

This feels like a bug in fiber_scheduler. Am I right?

(If it matters, I'm using Linux on x86_64, and I installed fiber_scheduler through Bundler.)

@ashtneoi
Copy link
Author

ashtneoi commented Aug 7, 2022

Does this patch look like a reasonable fix? It fixes this particular case, at least.

diff --git a/lib/fiber_scheduler/selector.rb b/lib/fiber_scheduler/selector.rb
index 068b390..172f18c 100644
--- a/lib/fiber_scheduler/selector.rb
+++ b/lib/fiber_scheduler/selector.rb
@@ -206,7 +206,7 @@ class FiberScheduler
         end
       end
 
-      duration = 0 if @ready.any?
+      duration = 0 if @ready.any? or (readable.empty? and writable.empty?)
       readable, writable, _ = IO.select(readable, writable, nil, duration)
 
       ready = Hash.new(0)

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

1 participant