Julia: Overflow in Timer when passing time as an Int

Created on 4 Oct 2019  Â·  6Comments  Â·  Source: JuliaLang/julia

I was trying to make a message be sent 18446744073709552 seconds (585 million years) into the future by doing the following:

julia> Timer(18446744073709552) do timer
          println("Hello distant friend")
       end;

However, the message prints immediately and my distant friend will not receive the greeting. :(

Most helpful comment

Yeah, a factor of 2. The easiest to me seems to just error if it overflows.

All 6 comments

I think something like this might help:

--- a/base/asyncevent.jl
+++ b/base/asyncevent.jl
@@ -71,8 +71,8 @@ mutable struct Timer
     function Timer(timeout::Real; interval::Real = 0.0)
         timeout ≥ 0 || throw(ArgumentError("timer cannot have negative timeout of $timeout seconds"))
         interval ≥ 0 || throw(ArgumentError("timer cannot have negative repeat interval of $interval seconds"))
-        timeout = UInt64(round(timeout * 1000)) + 1
-        interval = UInt64(round(interval * 1000))
+        timeout = round(UInt64, timeout) * 1000 + 1
+        interval = round(UInt64, interval) * 1000
         loop = eventloop()

         this = new(Libc.malloc(_sizeof_uv_timer), ThreadSynchronizer(), true, false)

Hm, why would that help? Seems the * 1000 can still easily overflow.

When timeout is signed, it doesn't have as much room in the upper range before overflow, so if we make it unsigned first, there's a bit more room to make it bigger before it overflows.

Yeah, a factor of 2. The easiest to me seems to just error if it overflows.

The easiest to me seems to just error if it overflows

Or just don't worry about starting the Timer if you ask for more than 1 millennium into the future

The problem is someone that puts a very long default time for the timer to keep the same code and it then fires early. I don't think all olqcew that start a timer should need to worry about overflow. Seems simple enough to fix.

Was this page helpful?
0 / 5 - 0 ratings