dotnet / dotnet/dotnet-api-docs

Erroroneous Information on BlockingCollection.GetConsumingEnumerable

Open
#4,653 2 comments 0 reactions 0 assignees View on GitHub
area-System.Collections Pri3
Dominant language
C#
Stars
949
Forks
1.7k
Avg merge
3d 27m
Merged PRs (30d)
49

Description

This page:

https://docs.microsoft.com/en-us/dotnet/api/system.collections.concurrent.blockingcollection-1.getconsumingenumerable?view=netcore-3.1

([github link](https://github.com/dotnet/dotnet-api-docs/blob/master/xml/System.Collections.Concurrent/BlockingCollection%601.xml))

States:

>This method enables client code to remove items from the collection by using a foreach loop (For Each in Visual Basic), or Parallel.ForEach or a PLINQ query.

But using this in a `Parallel.ForEach` loop causes significant problems when you near the end of the list. If you do not call the `CompleteAdding` method, then the last items can remain unprocessed. This has something to do with the partitioning (I am not entirely sure why). It is very common for a producer to not finish, but have a lull in production. When that happens, some of the items in the `BlockingCollection` remain unprocessed by the `Parallel.ForEach` (consumer) until the producer starts producing again (which could be a very long time), if ever.)

This stack overflow question and answer have more details https://stackoverflow.com/questions/17828518/parallel-foreach-stalled-when-integrated-with-blockingcollection

I recommend adding a caveat that using `Parallel.ForEach` with `BlockingCollection.GetConsumingEnumerable` it will cause unprocessed items when the `BlockingCollection` list runs out and `CompleteAdding` has not been called.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.