negative timeout in Rainbows::Fiber::Base

Eric Wong normalperson at yhbt.net
Sat Aug 25 02:45:56 UTC 2012


"Lin Jen-Shin (godfat)" <godfat at godfat.org> wrote:
> Greetings Unicorns,
> 
> I am writing some stupid benchmarks, trying FiberSpawn and
> FiberPool, and seeing this error:
> 
>     listen loop error: time interval must be positive (ArgumentError)
> 
> Looking into the code, in this line:
> http://bogomips.org/rainbows.git/tree/lib/rainbows/fiber/base.rb#n55
> Isn't that possible `max <= (now + 1)` and `now > max`?
> Whenever this is the case, then timeout passed to `select`
> could be negative. I am not sure how to fix this correctly,
> but a blind `abs` could resolve this error.

The original code looks wrong and confusing :x

It's been a while since I've looked hard at the Fiber* stuff (not a fan
of it personally).  However, I don't think abs() is correct, either.

> Hope this could give some hints, thank you!

Perhaps this is less confusing and more correct:

diff --git a/lib/rainbows/fiber/base.rb b/lib/rainbows/fiber/base.rb
index 6828e1d..103c97a 100644
--- a/lib/rainbows/fiber/base.rb
+++ b/lib/rainbows/fiber/base.rb
@@ -51,12 +51,15 @@ module Rainbows::Fiber::Base
       end
     }
     fibs.each { |fib| fib.resume }
-    now = Time.now
+
+    max_sleep = 1.0
     if max
-      timeout = max - now
-      timeout < 0.0 ? 0 : timeout
+      max -= Time.now
+      return 0 if max < 0.0
+      return max_sleep if max > max_sleep
+      max
     else
-      1
+      max_sleep
     end
   end
 
---
Thoughts?


More information about the rainbows-talk mailing list