BlockingCollection.GetConsumingEnumerable and Parallel.ForEach hang - by Mike Barry

Status : 

  Won't Fix<br /><br />
		Due to several factors the product team decided to focus its efforts on other items.<br /><br />
		A more detailed explanation for the resolution of this particular item may have been provided in the comments section.

Sign in
to vote
ID 674705 Comments
Status Closed Workarounds
Type Bug Repros 0
Opened 6/8/2011 2:24:16 PM
Access Restriction Public


Using a blocking collection GetConsumingEnumerable with Parallel.ForEach could never finish if only 1 item at a time is ever in the collection. (This is probably not the only case, just the first one I found.)
Sign in to post a comment.
Posted by Daniel Crenna on 11/19/2012 at 9:39 PM
The provided workaround is not acceptable as it requires all derivative works to comply with the MS-LPL, in other words, be completely tied down to Windows environments. This is not an OSI-approved license and accepting the workaround as given introduces all kinds of headaches for any solutions that ship cross-platform. The entire Rx framework is licenses under and OSI-approved license, as is ASP.NET. Please update the TPL examples to work with a real OSS license so that we can actually take your advice without introducing risk and vendor constraints our customers don't want.
Posted by Mike Barry on 8/22/2012 at 1:45 PM
Was this fixed in .NET 4.5?
Posted by Mike Barry on 6/9/2011 at 1:32 PM
Thanks. You guys rock as usual!
Posted by Microsoft on 6/9/2011 at 12:46 PM

This is a known problem. The issue is that the partitioning mechanism used in Parallel.ForEach consumes elements in "chunks"; chunk size starts at 1, then gradually grows. The internal logic grabs all elements in a chunk and then hands them back to Parallel.ForEach to process them as a group. In your example, for any chunk size greater than 1, the enumerator will hang because it can't grab the second element in the chunk until it processes the first element in the chunk.

See for a discussion of this problem, as well as some code (GetConsumingPartitioner() extension method and BlockingCollectionPartitioner<T> class) that allow you to work around the problem.

(And yes, we will add a workaround out-of-the-box in an upcoming version of .NET).

Hope this helps,
Posted by EricLeong [Feedback Moderator] on 6/8/2011 at 7:41 PM
Thank you for submitting feedback on Visual Studio 2010 and .NET Framework. Your issue has been routed to the appropriate VS development team for review. We will contact you if we require any additional information.
Posted by MS-Moderator01 on 6/8/2011 at 2:54 PM
Thank you for your feedback, we are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(