Bug 43447

Summary: SynchronizedCollection.Add throws an ArgumentOutOfRangeException
Product: [Mono] Class Libraries Reporter: Ryan Twitchell <metatheorem>
Component: WCF assembliesAssignee: Bugzilla <bugzilla>
Status: Tracked in Related Links ---    
Severity: normal CC: alkpli, mono-bugs+mono
Priority: ---    
Version: 4.4.1 (C7SR0)   
Target Milestone: Untriaged   
Hardware: PC   
OS: Linux   
Related Links: https://github.com/mono/mono/pull/10021
Tags: Is this bug a regression?: ---
Last known good build:
Attachments: Example code that produces this bug

Description Ryan Twitchell 2016-08-17 01:30:57 UTC
Created attachment 17068 [details]
Example code that produces this bug

During concurrent calls to Add and Remove on a SynchronizedCollection instance, Add throws an ArgumentOutOfRangeException with the message "Index must be within the bounds of the List. Parameter name: index".

This was with mono 4.4.1 originally, and a quick check of the code on your github master branch (https://github.com/mono/mono/blob/master/mcs/class/System.ServiceModel/System.Collections.Generic/SynchronizedCollection.cs) shows that this may be because Add reads its backing list's count outside of a lock, and passes that to InsertItem.

Example code attached.  Reproduces almost every run with mono, never with .NET 4.6.