Skip to content

Commit 3740172

Browse files
vtjnashstaticfloat
authored andcommitted
Timer: handle timeout correctly (#42854)
I am not sure why we ever used round+1 instead of ceil+1, as this is simply strictly more correct. (cherry picked from commit d6f59fa)
1 parent ba7e233 commit 3740172

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

base/asyncevent.jl

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,13 @@ end
5757
5858
Create a timer that wakes up tasks waiting for it (by calling [`wait`](@ref) on the timer object).
5959
60-
Waiting tasks are woken after an initial delay of `delay` seconds, and then repeating with the given
61-
`interval` in seconds. If `interval` is equal to `0`, the timer is only triggered once. When
62-
the timer is closed (by [`close`](@ref)) waiting tasks are woken with an error. Use [`isopen`](@ref)
63-
to check whether a timer is still active.
60+
Waiting tasks are woken after an initial delay of at least `delay` seconds, and then repeating after
61+
at least `interval` seconds again elapse. If `interval` is equal to `0`, the timer is only triggered
62+
once. When the timer is closed (by [`close`](@ref)) waiting tasks are woken with an error. Use
63+
[`isopen`](@ref) to check whether a timer is still active.
64+
65+
Note: `interval` is subject to accumulating time skew. If you need precise events at a particular
66+
absolute time, create a new timer at each expiration with the difference to the next time computed.
6467
"""
6568
mutable struct Timer
6669
handle::Ptr{Cvoid}
@@ -71,8 +74,9 @@ mutable struct Timer
7174
function Timer(timeout::Real; interval::Real = 0.0)
7275
timeout 0 || throw(ArgumentError("timer cannot have negative timeout of $timeout seconds"))
7376
interval 0 || throw(ArgumentError("timer cannot have negative repeat interval of $interval seconds"))
74-
timeout = UInt64(round(timeout * 1000)) + 1
75-
interval = UInt64(round(interval * 1000))
77+
# libuv has a tendency to timeout 1 ms early, so we need +1 on the timeout (in milliseconds), unless it is zero
78+
timeoutms = ceil(UInt64, timeout * 1000) + !iszero(timeout)
79+
intervalms = ceil(UInt64, interval * 1000)
7680
loop = eventloop()
7781

7882
this = new(Libc.malloc(_sizeof_uv_timer), ThreadSynchronizer(), true, false)
@@ -84,7 +88,7 @@ mutable struct Timer
8488
ccall(:uv_update_time, Cvoid, (Ptr{Cvoid},), loop)
8589
err = ccall(:uv_timer_start, Cint, (Ptr{Cvoid}, Ptr{Cvoid}, UInt64, UInt64),
8690
this, @cfunction(uv_timercb, Cvoid, (Ptr{Cvoid},)),
87-
timeout, interval)
91+
timeoutms, intervalms)
8892
@assert err == 0
8993
iolock_end()
9094
return this
@@ -219,18 +223,18 @@ end
219223
"""
220224
Timer(callback::Function, delay; interval = 0)
221225
222-
Create a timer that wakes up tasks waiting for it (by calling [`wait`](@ref) on the timer object) and
223-
calls the function `callback`.
226+
Create a timer that runs the function `callback` at each timer expiration.
224227
225-
Waiting tasks are woken and the function `callback` is called after an initial delay of `delay` seconds,
226-
and then repeating with the given `interval` in seconds. If `interval` is equal to `0`, the timer
227-
is only triggered once. The function `callback` is called with a single argument, the timer itself.
228-
When the timer is closed (by [`close`](@ref)) waiting tasks are woken with an error. Use [`isopen`](@ref)
229-
to check whether a timer is still active.
228+
Waiting tasks are woken and the function `callback` is called after an initial delay of `delay`
229+
seconds, and then repeating with the given `interval` in seconds. If `interval` is equal to `0`, the
230+
callback is only run once. The function `callback` is called with a single argument, the timer
231+
itself. Stop a timer by calling `close`. The `cb` may still be run one final time, if the timer has
232+
already expired.
230233
231234
# Examples
232235
233-
Here the first number is printed after a delay of two seconds, then the following numbers are printed quickly.
236+
Here the first number is printed after a delay of two seconds, then the following numbers are
237+
printed quickly.
234238
235239
```julia-repl
236240
julia> begin

0 commit comments

Comments
 (0)