Created
June 7, 2025 13:56
-
-
Save eriawan/65d38acbdd3649f394d6eff0f1faa434 to your computer and use it in GitHub Desktop.
Minimum code repro to the "problem of having intermittent exception of System.ArgumentExeption when adding item to List<T> inside Task.WhenAll"
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
| <Project Sdk="Microsoft.NET.Sdk"> | |
| <PropertyGroup> | |
| <OutputType>Exe</OutputType> | |
| <TargetFrameworks>net8.0;net9.0</TargetFrameworks> | |
| <ImplicitUsings>enable</ImplicitUsings> | |
| <Nullable>enable</Nullable> | |
| </PropertyGroup> | |
| </Project> |
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
| namespace DiskusiTestArrayExceptionDiTaskWhenAll | |
| { | |
| public class MiscelanousComputeProcessSimplified | |
| { | |
| public static List<Double> ProcessIterateFor(int iteration) | |
| { | |
| List<Double> result = new List<Double>(); | |
| Random random = new Random(255); | |
| for (int i = 0; i < iteration; i++) | |
| { | |
| double anycalc = Math.Pow(2.1, random.NextDouble()); | |
| result.Add(anycalc); | |
| Thread.Sleep(20); | |
| } | |
| return result; | |
| } | |
| public static async Task TaskWhenAll_ProcessIterateFor() | |
| { | |
| List<Task> tasklist = new List<Task>(); | |
| List<Double> randomResult = new List<Double>(); | |
| for (int i = 1; i <= 50; i++) | |
| { | |
| tasklist.Add(Task.Run(() => | |
| { | |
| var iterationResult = ProcessIterateFor(60); | |
| iterationResult.ForEach((resultItem) => randomResult.Add(resultItem)); | |
| })); | |
| } | |
| await Task.WhenAll(tasklist); | |
| } | |
| public static async Task TaskWhenAll_ProcessIterateFor_WithLock() | |
| { | |
| object lockObject = new object(); | |
| List<Task> tasklist = new List<Task>(); | |
| List<Double> randomResult = new List<Double>(); | |
| for (int i = 1; i <= 50; i++) | |
| { | |
| tasklist.Add(Task.Run(() => | |
| { | |
| lock (lockObject) | |
| { | |
| var iterationResult = ProcessIterateFor(60); | |
| iterationResult.ForEach((resultItem) => randomResult.Add(resultItem)); | |
| } | |
| })); | |
| } | |
| await Task.WhenAll(tasklist); | |
| } | |
| } | |
| } |
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
| namespace DiskusiTestArrayExceptionDiTaskWhenAll | |
| { | |
| internal class Program | |
| { | |
| static async Task Main(string[] args) | |
| { | |
| Console.WriteLine("Test Array Exception in Task.WhenAll!"); | |
| int exceptionOccurrence = 0; | |
| for (int i = 1; i <= 40; i++) | |
| { | |
| Console.WriteLine($"iteration number {i}"); | |
| try | |
| { | |
| await MiscelanousComputeProcessSimplified.TaskWhenAll_ProcessIterateFor(); | |
| } | |
| catch (Exception exc) | |
| { | |
| exceptionOccurrence++; | |
| Console.WriteLine($"Exception thrown! Exception type = {exc.GetType().FullName},{Environment.NewLine}Message = {exc.Message}"); | |
| } | |
| } | |
| Console.WriteLine($"number of exceptions = {exceptionOccurrence}"); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment