Skip to content

feat: add an opt-in strict timer order to the MockTimeSystem - #1194

Merged
vbreuss merged 3 commits into
mainfrom
feat/strict-timer-order
Nov 4, 2026
Merged

vbreuss merged 3 commits into
mainfrom
feat/strict-timer-order

Conversation

@vbreuss

@vbreuss vbreuss commented Nov 4, 2026

Copy link
Copy Markdown
Member

With auto-advance, the thread that moves the clock waits for a timer callback only until it was blocked for one second of real time, as the callback might wait for something that only a later timer delivers, and it waits at most one second for the creator of a timer to finish setting it up, e.g. for Task.Delay on the TimeProvider to register its cancellation. On a loaded machine, both can let a later timer fire before an earlier one completed, and a wrong guess does not fail: the timers silently fire in another order, e.g. a 30s delay completes before the 10s timeout that should cancel it. Tests on a virtual clock that depend on the order of their timers, like those of aweXpect, then fail only on busy CI runners.

MockTimeSystemOptions.EnableStrictTimerOrder() (5 seconds) and EnableStrictTimerOrder(TimeSpan callbackTimeout) make the thread that moves the clock wait for each callback until it completes or waits for the clock through the MockTimeSystem, without the one second heuristic. The timers that move the clock on their own and AdvanceToNextTimer also wait up to the timeout for the creator of a timer, and then move on like the real clock does, as a creator may never finish, e.g. a delay with the token of a disposed CancellationTokenSource.

A callback that blocks on something that only the clock can deliver can never complete in this mode. Instead of moving on silently, the scheduler fails after the timeout with a TimeoutException that names the due time of the timer: the code that moves the clock throws it, and so does every later move of the clock (AdvanceBy, SetTo, auto-advancing Task.Delay, Thread.Sleep and PeriodicTimer), AdvanceToNextTimer and WaitUntilIdle(Async), whose waiters are woken. An expired timeout of AdvanceToNextTimer fails the scheduler as well. The default mode is unchanged.

The scope is deliberately narrower than "callbacks never run concurrently": callbacks still run on their own threads, so that a callback can wait for the clock, and the existing heuristics (timeouts that do not move the clock, the set-up of Task.Delay on the TimeProvider) stay. Documented limitations: another thread that moves the clock at the same time can still move it past a running callback; when the failure happens in the background loop of auto-advance, awaiting a timer (e.g. a delay on the TimeProvider) never completes and only the next move of the clock or WaitUntilIdle reports it; a time change callback that moves the clock again holds the clock, so a timer callback that reads it in the meantime fails the strict order.

@vbreuss vbreuss self-assigned this Nov 4, 2026
@vbreuss
vbreuss added this pull request to stack #1195 November 4, 2026 14:00
@vbreuss
vbreuss force-pushed the feat/strict-timer-order branch from 7e7741c to 168f5ca Compare November 4, 2026 14:40
Base automatically changed from feat/timer-stepping-and-idle to main November 4, 2026 15:51
vbreuss and others added 3 commits November 4, 2026 16:51
With auto-advance, the thread that moves the clock waits for a timer callback only until it was blocked for one second of real time, as the callback might wait for something that only a later timer delivers, and it waits at most one second for the creator of a timer to finish setting it up, e.g. for `Task.Delay` on the `TimeProvider` to register its cancellation. On a loaded machine, both can let a later timer fire before an earlier one completed, and a wrong guess does not fail: the timers silently fire in another order, e.g. a 30s delay completes before the 10s timeout that should cancel it. Tests on a virtual clock that depend on the order of their timers, like those of aweXpect, then fail only on busy CI runners.

`MockTimeSystemOptions.EnableStrictTimerOrder()` (5 seconds) and `EnableStrictTimerOrder(TimeSpan callbackTimeout)` make the thread that moves the clock wait for each callback until it completes or waits for the clock through the `MockTimeSystem`, without the one second heuristic. The timers that move the clock on their own and `AdvanceToNextTimer` also wait up to the timeout for the creator of a timer, and then move on like the real clock does, as a creator may never finish, e.g. a delay with the token of a disposed `CancellationTokenSource`.

A callback that blocks on something that only the clock can deliver can never complete in this mode. Instead of moving on silently, the scheduler fails after the timeout with a `TimeoutException` that names the due time of the timer: the code that moves the clock throws it, and so does every later move of the clock (`AdvanceBy`, `SetTo`, auto-advancing `Task.Delay`, `Thread.Sleep` and `PeriodicTimer`), `AdvanceToNextTimer` and `WaitUntilIdle(Async)`, whose waiters are woken. An expired timeout of `AdvanceToNextTimer` fails the scheduler as well. The default mode is unchanged.

The scope is deliberately narrower than "callbacks never run concurrently": callbacks still run on their own threads, so that a callback can wait for the clock, and the existing heuristics (timeouts that do not move the clock, the set-up of `Task.Delay` on the `TimeProvider`) stay. Documented limitations: another thread that moves the clock at the same time can still move it past a running callback; when the failure happens in the background loop of auto-advance, awaiting a timer (e.g. a delay on the `TimeProvider`) never completes and only the next move of the clock or `WaitUntilIdle` reports it; a time change callback that moves the clock again holds the clock, so a timer callback that reads it in the meantime fails the strict order.

This builds on the idle tracking of the stepping and idle detection, which it is stacked on.
The XML docs of EnableStrictTimerOrder explain what it does, but not when to use it. They now name the typical case: tests that depend on the order of their timers, e.g. a timeout that must cancel a delay, and that fail only on a loaded machine.
Resolves the Sonar issue about an unobserved async result.
@vbreuss
vbreuss force-pushed the feat/strict-timer-order branch from 70ba869 to e74914d Compare November 4, 2026 15:51
@vbreuss
vbreuss merged commit 333d827 into main Nov 4, 2026
8 of 10 checks passed
@vbreuss
vbreuss deleted the feat/strict-timer-order branch November 4, 2026 15:52
@sonarqubecloud

sonarqubecloud Bot commented Nov 4, 2026

Copy link
Copy Markdown

@github-actions

Copy link
Copy Markdown

This is addressed in release v8.0.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant