SynchronizedCollection.Add throws an ArgumentOutOfRangeException

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.


Reference: https://bugzilla.xamarin.com/show_bug.cgi?id=43447