xamarin / Monkey.BluetoothLE

BluetoothLE abstraction for use with Xamarin projects, including Xamarin.Forms.
Apache License 2.0
348 stars 223 forks source link

GetServiceAsync is not thread safe #51

Open cprice70 opened 9 years ago

cprice70 commented 9 years ago

GetServiceAsync crashes with the following exception, it appears that the collection is being modified at the same time that it is trying to enumerate the services. The exception happens when this line is called

var s = device.Services.First (x => x.ID == id);

System.InvalidOperationException: Collection was modified; enumeration operation may not execute. at System.ThrowHelper.ThrowInvalidOperationException (ExceptionResource resource) [0x0000b] in :0 at System.Collections.Generic.List1+Enumerator[Robotics.Mobile.Core.Bluetooth.LE.IService].MoveNextRare () [0x00016] in <filename unknown>:0 at System.Collections.Generic.List1+Enumerator[Robotics.Mobile.Core.Bluetooth.LE.IService].MoveNext () [0x00050] in :0 at System.Linq.Enumerable.First[IService](IEnumerable1 source, System.Func2 predicate, Fallback fallback) [0x00026] in :0 at System.Linq.Enumerable.First[IService](IEnumerable1 source, System.Func2 predicate) [0x00007] in :0 at Robotics.Mobile.Core.Bluetooth.LE.Extensions+cAnonStorey1.<>m1 (System.Object sender, System.EventArgs e) [0x0002a] in /Users/cprice1/Projects/Monkey.Robotics/Source/Platform Stacks/Robotics.Mobile.Core/Bluetooth/LE/Extensions.cs:78 at --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000b] in :0 at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x0004e] in :0 at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x0002e] in :0 at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x0000b] in :0 at System.Runtime.CompilerServices.TaskAwaiter`1[Robotics.Mobile.Core.Bluetooth.LE.IService].GetResult () [0x00000] in :0 at IDS.BluetoothAdapter+casync0.MoveNext () [0x000a4] in /Users/cprice1/Projects/Control/Control/IDS/IDS.BluetoothAdapter.cs:175 at --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000b] in :0 at System.Runtime.CompilerServices.AsyncMethodBuilderCore.m1 (System.Object state) [0x00000] in :0