Skip to content

Stop equal polling and action intervals skipping a tick in FromLastCompletion [patch] - #96

Merged
matt-edmondson merged 2 commits into
mainfrom
fix/93-from-last-completion-skipped-ticks
Nov 4, 2026
Merged

matt-edmondson merged 2 commits into
mainfrom
fix/93-from-last-completion-skipped-ticks

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #93

Summary

PR #87 fixed skipped ticks for FromLastStart only. In the default FromLastCompletion mode, a run is stamped when it finishes (RecordRun), but the polling wait starts as soon as the run is dispatched. The next tick was therefore short of the run being due by however long the run took. With equal polling and action intervals, every run slipped a whole polling interval, so the action ran at half the requested rate.

Fix: WaitOutPollingIntervalAsync already tops its wait up to the full PollingInterval. It now also extends the wait until a full PollingInterval has passed since a FromLastCompletion run that finished during the wait. In effect, the polling cadence re-anchors to the completion, which is what the mode measures from.

  • The completion time is kept in a new LastCompletionTimestamp, set by RecordRun from the real Stopwatch. The wait sleeps in real time, so it shouldn't mix clocks with the replaceable GetTimestamp that tests inject for LastRunTimestamp.
  • FromLastStart never calls RecordRun, so its behaviour is unchanged.
  • A completion from before the wait started is ignored (completedAt > start), so a stale stamp from before a restart has no effect.

Tests

EqualPollingAndActionIntervalsSkipNoTicks now takes the interval type and the action's work time as parameters: FromLastStart with 0 and 20 ms, and FromLastCompletion with 0, 5 and 20 ms.

  • With the fix reverted: FromLastCompletion with 5 ms and with 20 ms both fail. Runs land at 3, 405, 809, 1211, 1612 ms and 0, 404, 805, 1208, 1615 ms against a 200 ms interval.
  • With the fix: all 5 cases pass in 3 of 3 runs.
  • Full IntervalAction.Test suite: 51 of 51 pass locally (Linux, .NET 10 SDK).

Generated by Claude Code

…mpletion [patch]

A FromLastCompletion run is stamped when it finishes, but the polling wait
started as soon as the run was dispatched. The next tick therefore fell
short of the run being due by however long the run took, so with equal
polling and action intervals every run slipped a whole polling interval and
the default mode ran the action at half the requested rate.

The polling wait now also lasts until a full polling interval has passed
since a run that finishes while it is in progress. The completion is
recorded on the real Stopwatch clock, apart from the replaceable
GetTimestamp the scheduling check reads, because the wait sleeps in real
time. The equal-intervals regression test now covers both interval types,
with actions that take 0, 5 and 20 ms.

Fixes #93

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vfJW24pNCmCLDhz6z3ZdY
…il it

On the macOS runner every case of EqualPollingAndActionIntervalsSkipNoTicks
failed, the pre-existing FromLastStart one included, with Task.Delay
oversleeping by up to about 200 ms: gaps of 335-427 ms against a 300 ms
bound, none of them a skipped tick. At 500 ms the 750 ms bound absorbs that
oversleep, while a skipped tick, about twice the interval, still exceeds it:
without the fix the FromLastCompletion cases still fail locally, with runs
1000 ms apart.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vfJW24pNCmCLDhz6z3ZdY
matt-edmondson pushed a commit that referenced this pull request Oct 31, 2026
…il it

The macOS runner failed EqualPollingAndActionIntervalsSkipNoTicks twice on
this PR, which does not touch scheduling, with Task.Delay oversleeping by
up to about 140 ms (gaps of 339-352 ms against a 300 ms bound, none a
skipped tick). At 500 ms the 750 ms bound absorbs that oversleep, while a
skipped tick, about twice the interval, still exceeds it. The same change
is on #96, which reworks this test, so the two merge cleanly.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vfJW24pNCmCLDhz6z3ZdY
@sonarqubecloud

Copy link
Copy Markdown

Copy link
Copy Markdown
Contributor Author

SonarCloud S2925 ("Do not use Thread.Sleep() in a test", IntervalActionTests.cs:1138): I'm leaving this as is.

That sleep is the body of the action under test, not the test waiting for a condition. #93 only shows up when the action takes real time to run, so the test makes the action block for 5 or 20 ms, the way a synchronous action would. With the fix reverted, those cases fail. The quality gate passed, and CI is green on all three OSes.


Generated by Claude Code

matt-edmondson added a commit that referenced this pull request Nov 4, 2026
* Reject an undefined IntervalType in Start [patch]

An IntervalType outside FromLastCompletion and FromLastStart, such as a
stale number bound from configuration, matched neither branch that records
a run. LastRunTimestamp stayed null, so the action ran on every poll and
ignored ActionInterval. Start now throws ArgumentOutOfRangeException for it,
the same way it rejects an out-of-range PollingInterval.

Fixes #94

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vfJW24pNCmCLDhz6z3ZdY

* Widen the equal-intervals test's interval so CI timer jitter can't fail it

The macOS runner failed EqualPollingAndActionIntervalsSkipNoTicks twice on
this PR, which does not touch scheduling, with Task.Delay oversleeping by
up to about 140 ms (gaps of 339-352 ms against a 300 ms bound, none a
skipped tick). At 500 ms the 750 ms bound absorbs that oversleep, while a
skipped tick, about twice the interval, still exceeds it. The same change
is on #96, which reworks this test, so the two merge cleanly.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vfJW24pNCmCLDhz6z3ZdY

---------

Co-authored-by: Claude <noreply@anthropic.com>
@matt-edmondson
matt-edmondson merged commit 4e7d4e3 into main Nov 4, 2026
15 checks passed
@matt-edmondson
matt-edmondson deleted the fix/93-from-last-completion-skipped-ticks branch November 4, 2026 00:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants