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
@@ -130,6 +130,8 @@ public sealed class VirtualTagActor : ReceiveActor, IWithUnboundedStash
private readonly bool _changeTriggered;
private readonly int? _timerIntervalMs;
private ICancelable? _timer;
// Gitea #1061: scheduler-clock time of the last tick that evaluated (null until the first).
private TimeSpan? _lastTimerEvaluation;
// One outstanding RunnerRecovered wait at most: a burst of Unavailable results re-evaluates once (latest-wins).
private bool _recoveryArmed;
// R02:P6 — a script failing on every trigger of a 1 Hz input published one cluster-wide DPS
@@ -248,7 +250,7 @@ public sealed class VirtualTagActor : ReceiveActor, IWithUnboundedStash
Receive<DependencyMuxActor.ProjectedDependencyWake>(wake => wake.Consume(items => OnDependencyBatch(new(items))));
Receive<ReassertValue>(_ => OnReassertValue());
Receive<RetrySourceBinding>(_ => RetrySource());
Receive<TimerTick>(_ => Evaluate(DateTime.UtcNow));
Receive<TimerTick>(_ => OnTimerTick());
Receive<RunnerRecovered>(_ =>
{
_recoveryArmed = false;
@@ -294,6 +296,24 @@ public sealed class VirtualTagActor : ReceiveActor, IWithUnboundedStash
_mux?.Tell(new DependencyMuxActor.UnregisterInterest(Self));
}
/// <summary>
/// A timer tick (lmxopcua#783). Gitea #1061: Akka's scheduler does not skip the ticks it missed while its thread
/// was stalled (a GC pause, CPU starvation) — it delivers every one of them in a single burst when it resumes (a
/// 600 ms stall ⇒ 12 ticks inside one millisecond). Each used to be a full evaluation, so one stall multiplied
/// every timer tag's script runs by the stall length over its interval. A tick that lands within half an interval
/// of the last timer evaluation is such a catch-up duplicate and is dropped; the regular cadence (one tick per
/// interval, the wheel's jitter a fraction of the 50 ms floor) is never affected. Ticks during a run are still
/// dropped by <see cref="Evaluating"/>.
/// </summary>
private void OnTimerTick()
{
var now = Context.System.Scheduler.MonotonicClock;
if (_lastTimerEvaluation is { } last && _timerIntervalMs is { } ms && now - last < TimeSpan.FromMilliseconds(ms / 2.0))
return;
_lastTimerEvaluation = now;
Evaluate(DateTime.UtcNow);
}
private void ScheduleSourceRetry()
{
_sourceBindingRetry?.Cancel();