title: Parallel.ForEach fails with "Object reference not set to an instance of an object." exception
body: <p>Very simple <code>Parallel.ForEach</code> call sometimes fails with a</p>
  <blockquote>
  <p>Object reference not set to an instance of an object.</p>
  </blockquote>
  <p>exception somewhere in the library and not in my code. Here is the stack trace.</p>
  <pre><code>  at System.Threading.Tasks.TaskReplicator.Run[TState](ReplicatableUserAction`1 action, ParallelOptions options, Boolean stopOnFirstFailure)
     at System.Threading.Tasks.Parallel.PartitionerForEachWorker[TSource,TLocal](Partitioner`1 source, ParallelOptions parallelOptions, Action`1 simpleBody, Action`2 bodyWithState, Action`3 bodyWithStateAndIndex, Func`4 bodyWithStateAndLocal, Func`5 bodyWithEverything, Func`1 localInit, Action`1 localFinally)
  --- End of stack trace from previous location where exception was thrown ---
     at System.Threading.Tasks.Parallel.ThrowSingleCancellationExceptionOrOtherException(ICollection exceptions, CancellationToken cancelToken, Exception otherException)
     at System.Threading.Tasks.Parallel.PartitionerForEachWorker[TSource,TLocal](Partitioner`1 source, ParallelOptions parallelOptions, Action`1 simpleBody, Action`2 bodyWithState, Action`3 bodyWithStateAndIndex, Func`4 bodyWithStateAndLocal, Func`5 bodyWithEverything, Func`1 localInit, Action`1 localFinally)
     at System.Threading.Tasks.Parallel.ForEachWorker[TSource,TLocal](IEnumerable`1 source, ParallelOptions parallelOptions, Action`1 body, Action`2 bodyWithState, Action`3 bodyWithStateAndIndex, Func`4 bodyWithStateAndLocal, Func`5 bodyWithEverything, Func`1 localInit, Action`1 localFinally)
     at System.Threading.Tasks.Parallel.ForEach[TSource](IEnumerable`1 source, Action`1 body)
  ...
  </code></pre>
  <p>It occurs very rarely and I have seen it happen only couple of times on .NET Core 2.1 App on Linux with no visible reason and no way to reproduce.</p>
  <p>Here is my simplified code:</p>
  <pre><code>var chunkSize = 100;
  var processingList = new List&lt;int&gt;();
  
  // removed code here which fill the processing list with data. No nulls here for sure
  
  Parallel.ForEach(
      ChunkIndexes(processingList.Count, chunkSize),
      (interval) =&gt;
      {
          var worker = new MyWorker();
          worker.Process(processingList, interval.startIndex, interval.endIndex);
      });
  </code></pre>
  <pre><code>// Function called by Parallel.ForEach()
  public static IEnumerable&lt;(int startIndex, int endIndex)&gt; ChunkIndexes(int totalCount, int chunkSize)
  {
      for (var i = 0; i &lt; totalCount; i += chunkSize)
      {
          yield return (i, Math.Min(i + chunkSize, totalCount));
      }
  }
  </code></pre>
  <p>The code in <code>Parallel.ForEach</code> is working on different pieces of the list and is only modifying values but not changing the list size in any way.
  There is no async/await code, no file access, no network calls either. It looks very simple and I just cannot figure out how it could ever fail for any reason. Any ideas?</p>