Skip to content

fix: hold the clock until Task.Delay on the TimeProvider registered its token - #1188

Merged
vbreuss merged 2 commits into
mainfrom
fix/delay-registration-race
Nov 3, 2026
Merged

vbreuss merged 2 commits into
mainfrom
fix/delay-registration-race

Conversation

@vbreuss

@vbreuss vbreuss commented Nov 3, 2026

Copy link
Copy Markdown
Member

Task.Delay(TimeSpan, TimeProvider, CancellationToken) registers its token only after it created its timer, so an auto-advancing clock could fire a timeout before the delay was registered and then run the delay to its end, although the timeout had canceled it. This made ToTimeProvider_Delay_WhenATimeoutCancelsIt_ShouldStopTheClockAtTheTimeout flaky and broke code that guards a delay with a timeout under load.

@vbreuss vbreuss self-assigned this Nov 3, 2026
@vbreuss
vbreuss enabled auto-merge (squash) November 3, 2026 00:25
@vbreuss
vbreuss force-pushed the fix/delay-registration-race branch from 67483cf to ba78d45 Compare November 3, 2026 00:25
…ts token

`Task.Delay(TimeSpan, TimeProvider, CancellationToken)` registers its token only after it created its timer, so an auto-advancing clock could fire a timeout before the delay was registered and then run the delay to its end, although the timeout had canceled it. This made `ToTimeProvider_Delay_WhenATimeoutCancelsIt_ShouldStopTheClockAtTheTimeout` flaky and broke code that guards a delay with a timeout under load.
@vbreuss
vbreuss force-pushed the fix/delay-registration-race branch from ba78d45 to 0a4e2e1 Compare November 3, 2026 01:08
@mergify

mergify Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

This pull request is merged.

@vbreuss
vbreuss merged commit 78955b7 into main Nov 3, 2026
10 of 11 checks passed
@vbreuss
vbreuss deleted the fix/delay-registration-race branch November 3, 2026 02:05
@sonarqubecloud

sonarqubecloud Bot commented Nov 3, 2026

Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed

Failed conditions
1 New issue

See analysis details on SonarQube Cloud

Catch issues before they fail your Quality Gate with our IDE extension SonarQube for IDE

vbreuss added a commit that referenced this pull request Nov 4, 2026
`Task.Delay(TimeSpan, TimeProvider, CancellationToken)` registers its token only after it created its timer, so the timers that advance the clock wait until the delay registered it (#1188). That wait was bounded by the same one second of real time as a blocked callback. On a loaded machine, the creator's thread may not run for that long, and the delay's timer then fires before its token is registered: a timeout that is due earlier no longer cancels the delay, and the timers silently fire in another order.

The wait now has its own limit of 5 seconds. It only takes that long for a creator that never finishes, e.g. a delay with the token of a disposed `CancellationTokenSource`, which never registers; such a delay now holds the clock for 5 instead of 1 second of real time, once for each such timer. This is documented on the auto-advance page. The wait still spins outside of all locks, so it blocks neither the creator nor other threads that move the clock.

A test that fails with the old limit pins the longer wait, and another one pins that a creator that never finishes does not stop the clock.
vbreuss added a commit that referenced this pull request Nov 4, 2026
…1196)

`Task.Delay(TimeSpan, TimeProvider, CancellationToken)` registers its token only after it created its timer, so the timers that advance the clock wait until the delay registered it (#1188). That wait was bounded by the same one second of real time as a blocked callback. On a loaded machine, the creator's thread may not run for that long, and the delay's timer then fires before its token is registered: a timeout that is due earlier no longer cancels the delay, and the timers silently fire in another order.

The wait now has its own limit of 5 seconds. It only takes that long for a creator that never finishes, e.g. a delay with the token of a disposed `CancellationTokenSource`, which never registers; such a delay now holds the clock for 5 instead of 1 second of real time, once for each such timer. This is documented on the auto-advance page. The wait still spins outside of all locks, so it blocks neither the creator nor other threads that move the clock.

A test that fails with the old limit pins the longer wait, and another one pins that a creator that never finishes does not stop the clock.
@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