Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot await Task.Delay with .Net Core under Linux

This very simple code does not work the same under Linux as it does on my Windows machine:

    class Program
    {
        async static Task Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            for (int i = 0; i < 5; i++)
            {
                await Task.Delay(TimeSpan.FromSeconds(1));
                Console.WriteLine("" + i);
            }

            Console.WriteLine("Bye bye");
        }
    }

This should produce an output like

Hello World

0

1

.. 

4

Bye bye

It works well in development and if published to Windows. If published for Linux it hangs after putting out 'Hello World'. The await Task.Delay() never returns.

I've tried with a fresh installation of Ubuntu 18 and CentOS 7. I've tried with ASP.Net Core 3.0 runtime and 3.1 (also both fresh installations).

This code is only for demonstration purposes. Originally, I started to have problems when using a System.Threading.Timer in an ASP.Net BackgroundService (registered via services.AddHostedService()). The timer's callback there also didn't got called. That also happened only under Linux, whereas Windows works well.

Does anyone have an idea how to approach this problem?

Update After the comment from Herohtar I've tested on a Windows subsystem Ubuntu. It is working there as well. Now I suspect the Linux image(s) of my hoster (virtual server at Strato.de) is somehow crippled.

Can someone think of a limitation in Linux that could cause such issues?

like image 603
ozerfrettelter Avatar asked Dec 21 '19 07:12

ozerfrettelter


People also ask

When do we use “task delay”?

Generally speaking we use Task.Delay to wait for specific amount of time in asynchronous fashion. I’m also going to explain why do we need to sometimes mimic this kind of behaviors. What Task.Delay Does?

How do I set the time delay of a task?

Most commonly, the time delay is introduced: At the beginning of the task, as the following example shows. C# Stopwatch sw = Stopwatch.StartNew (); var delay = Task. Sometime while the task is executing. In this case, the call to the Delay method executes as a child task within a task,...

What are the advantages of using wait another task?

If you use Wait another Task may try to enter the lock as well, and stop wait. Now, if it's not async, the thread will be locked and you have a deadlock, since the first Task, that has the lock, which is awaiting something else, is waiting for the thread to be released. So two advantages: 1. The thread is free to do other things. 2.

What happens when a thread is waiting in a task?

So instead of Thread.Sleepwe can use Task.Delay. By doing that when we wait, the thread can be used for other tasks. Which means while we’re waiting, the thread is released back to its caller or thread pool and no resource is wasted in the process.


1 Answers

After getting response from Strato I can post the answer to my own question here: Strato is using Virtuozzo for their virtualization. For security reasons they have also disabled some features so that .Net core ist not running there.

For everyone else having the same problem: You can't use .Net core on Strato Linux V-Servers at all.

Thaks to everyone trying to help!

like image 149
ozerfrettelter Avatar answered Sep 21 '22 09:09

ozerfrettelter