Bug 24213 - ConcurrentBag.TryTake returns the same instance twice...
Summary: ConcurrentBag.TryTake returns the same instance twice...
Alias: None
Product: Class Libraries
Classification: Mono
Component: System ()
Version: master
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: marcos.henrich
Depends on:
Reported: 2014-11-02 10:14 UTC by mikey10
Modified: 2014-11-20 09:58 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 mikey10 2014-11-02 10:14:12 UTC
One of my unit tests of an object pool implementation based on ConcurrentBag failed mysterious even on a non-concurrent tests...
Tracked down to something curiously at the ConcurrentBag. 

If u put 2049 unique object instances in a ConcurrentBag (<=2048 works fine...) and take out all of them, the first and the last taken object are equal ... scary...

Code to reproduce below...

var list = new List<Object>();
var bag = new ConcurrentBag<Object>();
for( int i = 0; i < 2049; i++ )
bag.Add( new Object() );

Assert.AreEqual( 2049, bag.Count );

for( int i = 0; i < 2049; i++ )
Object b = null;
Assert.IsTrue( bag.TryTake( out b ) );
list.Add( b );

Assert.IsFalse( Object.ReferenceEquals( list[0], list[2047] ), "first assert" );
Assert.IsFalse( Object.ReferenceEquals( list[0], list[2048] ), "second assert" );
Comment 1 marcos.henrich 2014-11-20 09:32:06 UTC
Hi Mikey,

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-11-20 09:58:04 UTC
Fixed in master de75999a8ad897f0b229b0a570a5a10302c03f3e.