diff --git a/src/Extensions/ParallelForEachExtensions.cs b/src/Extensions/ParallelForEachExtensions.cs index 4c7d7f8..fa20ccf 100644 --- a/src/Extensions/ParallelForEachExtensions.cs +++ b/src/Extensions/ParallelForEachExtensions.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.ComponentModel; +using System.Linq; using System.Threading; using System.Threading.Tasks; @@ -76,11 +77,12 @@ public List ReadExceptions() _exceptionListLock.Enter(ref lockTaken); try { - return _exceptionList; + // Return a copy, so the list being returned will not be modified + // by tasks that are still running if the loop was canceled + return new List(_exceptionList ?? Enumerable.Empty()); } finally { - _exceptionList = null; _exceptionListLock.Exit(useMemoryBarrier: false); } }