Skip to content

Task.Delay freezes on DateTime jumps #123950

@hlippke

Description

@hlippke

Description

When using Task.Delay, the runtime freezes if the time is set backwards.

Reproduction Steps

Run the following code:

// See https://aka.ms/new-console-template for more information

using System;
using System.Threading.Tasks;

var counter = 0;
while (true)
{
    await Task.Delay(TimeSpan.FromSeconds(1));
    Console.WriteLine($"{DateTime.Now:s}: {++counter:0000}");
}

And set the time back, e.g. using sudo date -s "2026-02-03 17:29:00".

Expected behavior

Monotonic output of the DateTime and counter

Actual behavior

Output stops, ie. Task.Delay does not return until the original time (+1s in this example) is reached

Regression?

This works in .net 8 and 9 on Linux and on all .net recent versions running on Windows.

Known Workarounds

No response

Configuration

.net runtime 10.0.0
SDK 10.0.102
Manjaro Linux 6.12.68-1 on VirtualBox 7.1.6 x64

and
.net runtime 10.0.2
SDK 10.0.102
Yocto Linux 6.11.4-fslc-fslc-g0ed66f981bd8 #1 SMP PREEMPT Mon Oct 21 14:14:25 UTC 2024 armv7l GNU/Linux

Other information

I have failed to find any means to sleep using a monotonic time source. System.Threading.Timer, PeriodicTimer, Thread.Sleep, ManualResetEventSlim.Wait - all are showing the same effect.

I was able to create a platform dependent wait method using poll from libc via P/Invoke

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions