Repository navigation
Description
What's wrong
#70 reported skipped ticks for FromLastStart and said that "FromLastCompletion behaves the same way". PR #87 closed it. That PR stamps a FromLastStart run at the moment its tick judged it due (TryBeginRun, IntervalAction/IntervalAction.cs:609-628), but it left FromLastCompletion unchanged. FromLastCompletion is the default IntervalType.
Two clocks disagree in FromLastCompletion mode:
- A run is stamped when the action finishes:
RecordRun(:725-731), called from:577-580and:657-660. - The next polling wait is measured from just after
TryRundispatched the run:WaitOutPollingIntervalAsync(:493-503), called at:450-454.
So at the next tick, elapsed time ≈ PollingInterval + timer overshoot − (dispatch delay + action duration).
When ActionInterval == PollingInterval, that falls short of ActionInterval whenever the action takes longer than the timer overshoot, which is about 1 ms. HasIntervalElapsed (:719-720) then returns false and the run slips a whole polling interval.
The first gap is always lost this way, even for an action that does no work, because the first dispatch is slow (JIT and thread-pool warm-up).
Reproduction (net10.0, Linux, HEAD 1a84c72)
var sw = Stopwatch.StartNew(); var times = new ConcurrentQueue<long>();
using var ia = IntervalAction.Start(new() {
PollingInterval = TimeSpan.FromSeconds(1), ActionInterval = TimeSpan.FromSeconds(1),
// IntervalType left at its default, FromLastCompletion
Action = () => { times.Enqueue(sw.ElapsedMilliseconds); Thread.Sleep(5); } });
await Task.Delay(6050); // print gaps between consecutive entriesEach row was observed in 3 of 3 runs:
| Action work | Runs | Gaps (ms) | Expected |
|---|---|---|---|
| 5 ms, 6 s window | 4 | 2003, 2001, 2005 | ~7 runs about 1000 ms apart |
| 20 ms, 10 s window | 6 | every gap 2000–2006 | ~11 runs |
| 0 ms, 10 s window | 9–10 | first gap always 2003–2006, the rest ~1000 | 11 runs |
For comparison, FromLastStart with 20 ms of work over 10 s gives 11 runs with gaps of 995–1001 ms.
Why it matters
The defaults are FromLastCompletion with a 1 s PollingInterval. Pairing them with a 1 s ActionInterval is the natural configuration, and in it any real action runs at half the requested rate.
#70's acceptance criterion was "equal polling and action intervals over about 10 periods shows no gap of 1.5 × ActionInterval or more". The default mode still fails it. The regression test EqualPollingAndActionIntervalsSkipNoTicks covers only FromLastStart.
Suggested fix / acceptance criteria
- Keep the next tick from landing just short of the due time in
FromLastCompletion. Some options:- When a run completes and stamps itself, wake the loop by cancelling or replacing the delay, then wait
min(PollingInterval, time until due). - After a completion, wait only until the run is due when that falls inside the next poll.
- When a run completes and stamps itself, wake the loop by cancelling or replacing the delay, then wait
- Add a
FromLastCompletionvariant ofEqualPollingAndActionIntervalsSkipNoTickswhose action sleeps about 5–20 ms. It should show no gap of 1.5 ×ActionIntervalor more over about 10 periods, and that includes the first gap.
Activity
matt-edmondson commentedon Oct 31, 2026
Triage
IntervalType, with equal polling and action intervals (the natural configuration), runs any real action at half the requested rate. This is the library's core behaviour.RecordRun/WaitOutPollingIntervalAsyncinIntervalAction.cs).FromLastStart. No open PR found.EqualPollingAndActionIntervalsSkipNoTicksshould be parameterised over both interval types.Generated by Claude Code
Give the #93 skipped-tick test a 1 s interval so macOS timer overslee…
Keep FromLastCompletion polling on its grid when the next run is due …