Bug 22775 - Flawed implementation of BlockingCollection(T).AddToAny - Only blocks on first collection
Summary: Flawed implementation of BlockingCollection(T).AddToAny - Only blocks on firs...
Alias: None
Product: Class Libraries
Classification: Mono
Component: System ()
Version: master
Hardware: All All
: --- normal
Target Milestone: Untriaged
Assignee: marcos.henrich
Depends on:
Reported: 2014-09-09 10:08 UTC by Kenneth
Modified: 2014-10-08 16:51 UTC (History)
2 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Kenneth 2014-09-09 10:08:38 UTC
BlockingCollection(T).AddToAny is supposed to block until ANY of the BlockingCollections in the collections parameter has space for an item. The current implementation simply blocks on the first collection.

Related ticket for the TakeFromAny implementation is #6095.

Related source code from mcs/class/System/System.Collections.Concurrent/BlockingCollection.cs#292:
public static int AddToAny (BlockingCollection<T>[] collections, T item)
    CheckArray (collections);
    int index = 0;
    foreach (var coll in collections) {
        try {
            coll.Add (item);
            return index;
        } catch {}
    return -1;

Since coll.Add(item) will block when the collection is full, only the first collection will ever get filled.
The exception only occurs on timeouts (which are not used here) or if the queue has been marked as completed.
Comment 1 marcos.henrich 2014-10-08 07:30:03 UTC
Hi Kenneth,

Thank you for the detailed bug report.

The pull request for this issue can be found in the link below.
Comment 2 marcos.henrich 2014-10-08 16:51:51 UTC
Fixed in master 114a27e57c856a272af34f2b2ea190f15f11cd06.