Repository navigation
fix: hold the clock until Task.Delay on the TimeProvider registered its token - #1188
Merged
Merged
Conversation
vbreuss
enabled auto-merge (squash)
November 3, 2026 00:25
vbreuss
force-pushed
the
fix/delay-registration-race
branch
from
November 3, 2026 00:25
67483cf to
ba78d45
Compare
…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
force-pushed
the
fix/delay-registration-race
branch
from
November 3, 2026 01:08
ba78d45 to
0a4e2e1
Compare
|
This pull request is merged. |
|
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.
|
This is addressed in release v8.0.0. |
This was referenced Nov 11, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.




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 madeToTimeProvider_Delay_WhenATimeoutCancelsIt_ShouldStopTheClockAtTheTimeoutflaky and broke code that guards a delay with a timeout under load.