0

I work with a server-side application written in C# and I perform a simple Task.Run like this:

{
...
Task.Run(() => DeleteFile(path));
...
}

It worked for years, but since yesterday it stopped working and the DeleteFile method is not called at all. The execution just continues to the next line of code. I tried Task.Run(() => File.Delete(path)), which doesn't work as well. Then I added Task.Wait as follows (with an empty action - just for test):

Task t = Task.Run(() => {});
t.Wait();

When the execution reaches t.Wait() it waits forever and doesn't go to the next line of code.

Other places in the application where I use Task.Run work properly.

What might be the reason?

chwarr
  • 6,777
  • 1
  • 30
  • 57
  • Please share DeleteFile code....i don't think that task is an issue. – mukesh kudi May 01 '19 at 15:58
  • 1
    Await it instead of using `t.Wait()`. – FCin May 01 '19 at 15:58
  • The `Wait()` waits forever because you are [blocking on async code](https://blog.stephencleary.com/2012/07/dont-block-on-async-code.html). Your original fire-and-forget version most likely stopped working because it now produces an exception [which you cannot see](https://stackoverflow.com/q/32067034/11683). – GSerg May 01 '19 at 15:58
  • Are you *absolutely sure* that `DeleteFile` isn't being called, vs something *inside* it failing? The next thing to look at would be "sync-context" - does your server have a sync-context? Did someone perhaps add one? What does `SynchronizationContext.Current` report? – Marc Gravell May 01 '19 at 15:59
  • 1
    @GSerg calling `Wait()` is a terrible idea, but it shouldn't actually make it *fail* unless there is a sync-context deadlock (or similar); this is not an endorsement of `.Wait()`, note! – Marc Gravell May 01 '19 at 16:00
  • @MarcGravell The OP says it "waits forever". That is what I meant. Poor choice of words. – GSerg May 01 '19 at 16:01
  • Note, a *perceived* indefinite delay here can be caused by thread-pool starvation; this is a: unlikely in most cases, and b: would usually cause a *delay* rather than a hard *block* (although there *are* cases where you can cause a spiral of death where your work item will never get processed) – Marc Gravell May 01 '19 at 16:03
  • Do you need `Task.Run()` to delete a file? Or there's more to it? Do you have some error handling in that method? Have you debugged it or logged the results? – Jimi May 01 '19 at 16:26
  • "Are you absolutely sure that DeleteFile isn't being called, vs something inside it failing?" Yes. Even if I call an empty method it's not being called. Even if I call standard File.Delete() instead of my method it's not being called. "calling Wait() is a terrible idea" - I don't call Wait(). I've just added it for test to see what happens. "Do you need Task.Run() to delete a file?" No. When I call DeleteFile or just File.Delete() it works fine. This is not my code. I can remove this Task.Run(), but I'm really concerned that something that worked suddenly stopped working. – Yevgeny Granat May 02 '19 at 06:26
  • "What does SynchronizationContext.Current report?" null – Yevgeny Granat May 02 '19 at 06:34
  • Investigating further I saw that this Task.Run() is inside (in methods' chain) another Task.Factory.StartNew(). Maybe this "contributes" to the problem as well. But it worked for years, so I still want to know the reason why it stopped working. For now I removed Task.Run() and all works fine. – Yevgeny Granat May 06 '19 at 09:18

1 Answers1

-3

Probably you have a Dead-Lock. See this link https://michaelscodingspot.com/c-deadlocks-in-depth-part-1/ . You need to really know when to call .Wait() .Result() .Run(), this three can crash your entire aplication.

redPanda
  • 1
  • 3