Skip to content

With the default FromLastCompletion, equal polling and action intervals still skip ticks: a 5 ms action set to every 1 s runs every 2 s #93

Closed

Description

@matt-edmondson

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-580 and :657-660.
  • The next polling wait is measured from just after TryRun dispatched 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 entries

Each 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.
  • Add a FromLastCompletion variant of EqualPollingAndActionIntervalsSkipNoTicks whose action sleeps about 5–20 ms. It should show no gap of 1.5 × ActionInterval or more over about 10 periods, and that includes the first gap.

Activity

  1. added
    bug
    on Oct 31, 2026
  2. matt-edmondson commented on Oct 31, 2026

    @matt-edmondson
    ContributorAuthor

    Triage


    Generated by Claude Code

  3. added 2 commits that reference this issue on Nov 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions