ppy/osu-framework

System.ArgumentException: Destination array is not long enough to copy all the items in the collection. Check array index a...

ppy-sentryintegration opened this issue · 4 comments

Sentry Issue: OSU-VDX

System.ArgumentException: Destination array is not long enough to copy all the items in the collection. Check array index and length.
  ?, in void BindableDictionary<TKey, TValue>.System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<TKey,TValue>>.CopyTo(?)
  ?, in void BindableDictionary<TKey, TValue>.Parse(object input, IFormatProvider provider)
  ?, in void BindableDictionary<TKey, TValue>.BindTo(BindableDictionary<TKey, TValue> them)
  ?, in void BindableDictionary<TKey, TValue>.osu.Framework.Bindables.IBindableDictionary<TKey,TValue>.BindTo(?)
  ?, in void CurrentlyOnlineDisplay.LoadComplete()
...
(9 additional frame(s) were not displayed)

An unhandled error has occurred.

The bindable dictionary being bound to is updated from network requests: https://github.com/ppy/osu/blob/41c33f74f27822889290aea330ade6654eb6f5a3/osu.Game/Online/Metadata/OnlineMetadataClient.cs#L188-L199.

The underlying issue is very likely a race condition when adding new items to the bindable dictionary during the BindTo() call.
Locking on them before BindTo() will probably resolves the osu! issue.

Isolated repro (100% rate in Release configuration, Debug takes a few tries):

[Test]
public void TestThreading()
{
    var d = new BindableDictionary<int, string>();
    var underTest = new BindableDictionary<int, string>();

    var startExecution = new ManualResetEventSlim();

    Task.Factory.StartNew(() =>
    {
        startExecution.Wait(TimeSpan.FromSeconds(10));

        for (int i = 0; i < 1000; i++)
            d.Add(i, $"test {i}");
    });

    var t = Task.Factory.StartNew(() =>
    {
        startExecution.Wait(TimeSpan.FromSeconds(10));

        underTest.BindTo(d);
    });

    startExecution.Set();

    t.Wait(TimeSpan.FromSeconds(10));
}

Stack trace from test matches the sentry report:

osu.Framework.Tests.Bindables.BindableDictionaryTest.TestThreading

System.AggregateException : One or more errors occurred. (Destination array is not long enough to copy all the items in the collection. Check array index and length.)
  ----> System.ArgumentException : Destination array is not long enough to copy all the items in the collection. Check array index and length.
   at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
   at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
   at System.Threading.Tasks.Task.Wait(TimeSpan timeout)
   at osu.Framework.Tests.Bindables.BindableDictionaryTest.TestThreading() in C:\...\osu-framework\osu.Framework.Tests\Bindables\BindableDictionaryTest.cs:line 1050
--ArgumentException
   at System.Collections.Generic.Dictionary`2.CopyTo(KeyValuePair`2[] array, Int32 index)
   at System.Collections.Generic.Dictionary`2.System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<TKey,TValue>>.CopyTo(KeyValuePair`2[] array, Int32 index)
   at osu.Framework.Bindables.BindableDictionary`2.System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<TKey,TValue>>.CopyTo(KeyValuePair`2[] array, Int32 arrayIndex) in C:\...\osu-framework\osu.Framework\Bindables\BindableDictionary.cs:line 249
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at osu.Framework.Bindables.BindableDictionary`2.Parse(Object input, IFormatProvider provider) in C:\...\osu-framework\osu.Framework\Bindables\BindableDictionary.cs:line 291
   at osu.Framework.Bindables.BindableDictionary`2.BindTo(BindableDictionary`2 them) in C:\...\osu-framework\osu.Framework\Bindables\BindableDictionary.cs:line 465
   at osu.Framework.Tests.Bindables.BindableDictionaryTest.<>c__DisplayClass64_0.<TestThreading>b__1() in C:\...\osu-framework\osu.Framework.Tests\Bindables\BindableDictionaryTest.cs:line 1045
   at System.Threading.Tasks.Task.InnerInvoke()
   at System.Threading.Tasks.Task.<>c.<.cctor>b__272_0(Object obj)
   at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location ---
   at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)

Bindables aren't thread safe, so I'd say this is up to us to fix osu! side..

#5635 is related

#5635 is related

I'd argue it's best to just cover this in that thread, and fix the issue osu! side for this one (I'll look into it).