2

I am getting this as an error in Visual Studio 2022

Avoid unsupported fire-and-forget async-void methods or delegates. Unhandled exceptions will crash the process.

Having a lot of trouble finding anything on this. The code is working 100% fine but I would like to avoid this error without disabling it if I can. The code line is along these terms.

Parallel.ForEach(things, async (thing) => await ProcessThing(thing));
Theodor Zoulias
  • 34,835
  • 7
  • 69
  • 104
Morgeth888
  • 143
  • 2
  • 13
  • 1
    Related: [Parallel foreach with asynchronous lambda](https://stackoverflow.com/questions/15136542/parallel-foreach-with-asynchronous-lambda) – Theodor Zoulias Sep 09 '22 at 14:37
  • For context, in my case, this is not a "fire-and-forget" I am very interested in the Processing working. – Morgeth888 Sep 09 '22 at 14:38
  • 1
    The `Parallell.ForEach` with asynchronous lambda results in fire-and-forget, unless the `ProcessThing` has a synchronous implementation (which would be a problem on its own). This means that when the `Parallell.ForEach` completes, not all launched asynchronous operations will be completed. – Theodor Zoulias Sep 09 '22 at 14:44
  • 1
    In .NET 6.0 this compile without any warnings – Marco Beninca Sep 09 '22 at 14:53
  • 1
    This is starting to make a lot sense thanks @TheodorZoulias. I think the real issue here is simply that Parallel.ForEach + async = bad, it can very well not do the work you are needing it to do. Will attempt the ForEachAsync method (if it was a snake it would have bit me) and will update this later on this. – Morgeth888 Sep 09 '22 at 14:57
  • Yes, it's definitely bad. If you think that the [linked question](https://stackoverflow.com/questions/15136542/parallel-foreach-with-asynchronous-lambda) addresses sufficiently your question, you could consider closing this one as a duplicate of the other (by clicking the 'Close' under the question's tags). – Theodor Zoulias Sep 09 '22 at 15:03
  • I don't think its a duplicate because it is focused on the visual studio error text, so I would like it to exist and hopefully help the next me find help on this topic by searching it. Any answer related to the code ought to refer to the question you added. – Morgeth888 Sep 09 '22 at 15:11

1 Answers1

3

Credit to theodor for pointing me to the underlying code issue addressed here in detail Parallel foreach with asynchronous lambda

The simple answer is that it is very unlikely your intention to start a bunch of back ground task by feeding Parallel.ForEach an async lambda, then continue the main thread. It will fire them off then continue the main thread likely not completing all the work you desire. Long story short Paralell.ForEach + async = Bad, try something else. The error from visual studio here is detecting a fire-and-forget, but it would be really help to call out risks associated with that such as not finishing the work as well as potentially crashing the process at a later point on the main thread. Below is what I chose as it is the most direct simple change from the old to the new.

var options = new ParallelOptions {
            MaxDegreeOfParallelism = 10,
            CancellationToken = CancellationToken.None
        };
await Parallel.ForEachAsync(things, options, async (thing, cancellationToken) => await ProcessThing(thing));

What might help this click is to view each "await" word as, stop here until this is done, with the original version (Paralell.ForEach) lacking that it simply continues after creating each of the tasks and not waiting on them to finish.

Morgeth888
  • 143
  • 2
  • 13