Skip to content

Commit

Permalink
bugfix: check @nextExpiration after the changes are committed (#3883)
Browse files Browse the repository at this point in the history
There was a slightly buggy behaviour with setting the global timer from `@timer_helper`. We observed that for each one-off timer set there were two expirations happening. The reason is that we were computing the next expiration before the current batch of expired timers were expunged. This re-added the same time as before, and thus caused the second (in-vain) wakeup.

The fix is of course to compute the next global expiration _after_ the already expired nodes were expunged.
  • Loading branch information
ggreif authored Mar 15, 2023
1 parent e0ddfb1 commit ac9a920
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions src/prelude/internals.mo
Original file line number Diff line number Diff line change
Expand Up @@ -519,15 +519,8 @@ func @timer_helper() : async () {
func Array_init<T>(len : Nat, x : T) : [var T] {
(prim "Array.init" : <T>(Nat, T) -> [var T])<T>(len, x)
};
let now = (prim "time" : () -> Nat64)();
let exp = @nextExpiration @timers;
let prev = (prim "global_timer_set" : Nat64 -> Nat64) exp;
// debug { assert prev == 0 };
if (exp == 0) {
return
};
let now = (prim "time" : () -> Nat64)();
var gathered = 0;
let thunks = Array_init<?(() -> async ())>(10, null); // we want max 10
Expand Down Expand Up @@ -565,8 +558,15 @@ func @timer_helper() : async () {
gatherExpired(@timers);
for (k in thunks.keys()) {
ignore switch (thunks[k]) { case (?thunk) ?thunk(); case _ null };
let exp = @nextExpiration @timers;
ignore (prim "global_timer_set" : Nat64 -> Nat64) exp;
if (exp == 0) @timers := null;
for (o in thunks.vals()) {
switch o {
case (?thunk) { ignore thunk() };
case _ { }
}
}
};
Expand Down

0 comments on commit ac9a920

Please sign in to comment.