fix(virtualtags): #1061 a scheduler catch-up burst evaluates a timer tag once, not once per missed tick

Root cause of the intermittent VirtualTagActorTimerTests coalesce failure (full suite only):
Akka's HashedWheelTimer does not skip ticks it missed while its thread was stalled (GC pause,
CPU starvation) - it delivers them all in one burst when it resumes. Measured with a parked
scheduler thread: a 600 ms stall => 12 ticks within the same millisecond. Ticks that fell due
during the gated run therefore landed just after the release, each a fresh evaluation, and broke
the post-release bound. In production the same burst multiplied every timer tag's script runs by
stall/interval after any stall.

VirtualTagActor now drops a tick that lands within half an interval of the last timer
evaluation (scheduler monotonic clock); the regular cadence is unaffected. New deterministic
test parks the scheduler thread for 600 ms with the release inside the stall: failed 3/3
before the fix, passes after, with a cadence positive control. The in-flight assertion
(Calls == 1 while gated) has no such mechanism - ticks during a run are dropped - and did not
fail in 3 full-suite + 12 class-scoped trx runs under load average ~490.
This commit is contained in:
Joseph Doherty
2026-12-03 15:25:10 -05:00
parent e967042fbb
commit ca5ee2ac57
3 changed files with 59 additions and 2 deletions
@@ -72,6 +72,43 @@ public sealed class VirtualTagActorTimerTests : RuntimeActorTestBase
$"coalesced: only real ticks may land after the release ({sinceRelease.ElapsedMilliseconds} ms since release)");
}
/// <summary>
/// Gitea #1061 — the real cause of the intermittent full-suite failure above. Akka's scheduler does not skip the
/// ticks it missed while its thread was stalled (a GC pause, CPU starvation under a loaded run): it delivers them
/// all in ONE burst when it resumes (measured: a 600 ms stall ⇒ 12 ticks within the same millisecond). Ticks that
/// fell due during a gated run therefore reached the actor just after the run, each one a fresh evaluation. The
/// stall is produced deterministically here by parking the scheduler's own thread, with the release inside it.
/// A catch-up burst must collapse to one evaluation; the cadence afterwards is unaffected (positive control).
/// </summary>
[Fact]
public void A_scheduler_catch_up_burst_after_a_slow_run_evaluates_once_not_once_per_missed_tick()
{
var parent = CreateTestProbe();
var evaluator = new CountingEvaluator(gateFirst: true);
parent.ChildActorOf(VirtualTagActor.Props("vt-1", "expr", evaluator: evaluator, timerIntervalMs: 50), TestContext.Current.CancellationToken);
AwaitCondition(() => evaluator.Calls == 1, PresenceBudget, TestContext.Current.CancellationToken);
// Park the scheduler's thread for 600 ms (~12 intervals) and finish the slow run inside that stall.
using var stalled = new ManualResetEventSlim();
using var resumed = new ManualResetEventSlim();
Sys.Scheduler.Advanced.ScheduleOnce(TimeSpan.Zero, () => { stalled.Set(); Thread.Sleep(600); resumed.Set(); });
stalled.Wait(TestContext.Current.CancellationToken);
evaluator.Release();
parent.ExpectMsg<VirtualTagActor.EvaluationResult>(TimeSpan.FromSeconds(5), cancellationToken: TestContext.Current.CancellationToken);
resumed.Wait(TestContext.Current.CancellationToken);
var sinceResume = System.Diagnostics.Stopwatch.StartNew();
// The burst lands now. Coalesced: one catch-up evaluation plus whatever real ticks elapsed meanwhile.
Thread.Sleep(100);
var elapsedTicks = (int)(sinceResume.ElapsedMilliseconds / 50) + 1;
evaluator.Calls.ShouldBeLessThanOrEqualTo(1 + 1 + elapsedTicks,
$"a catch-up burst ran once per missed tick ({sinceResume.ElapsedMilliseconds} ms since the scheduler resumed)");
// Positive control: the timer still evaluates on its cadence afterwards.
var before = evaluator.Calls;
AwaitCondition(() => evaluator.Calls >= before + 2, PresenceBudget, TestContext.Current.CancellationToken);
}
[Fact]
public void The_timer_stops_when_the_actor_stops()
{