WPF Crash with no stacktrace

Viewed 2064

I have a big application that randomly throws an exception. This exception has no reproduction path, and no clear stacktrace in the logs:

AppDomain.CurrentDomain.UnhandledException System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
at System.Collections.Generic.List`1.Enumerator.MoveNextRare()
at System.Collections.Generic.List`1.Enumerator.MoveNext()
at System.Windows.Data.ListCollectionView.RestoreLiveShaping()
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
at System.Windows.Threading.DispatcherOperation.InvokeImpl()
at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Windows.Threading.DispatcherOperation.Invoke()
at System.Windows.Threading.Dispatcher.ProcessQueue()
at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
at System.Windows.Threading.Dispatcher.Run()
at System.Windows.Application.RunDispatcher(Object ignore)
at System.Windows.Application.RunInternal(Window window)
at System.Windows.Application.Run(Window window)
at System.Windows.Application.Run()
at FullDownloadManager.App.Main()

As you can see it is a concurrency problem with some collection being changed while UI thread refreshes and iterates through the list.

The problem is to find where is it happening since there is no track of where it occurred.

Does anyone knows a way to detect where this exception is being thrown?

Thanks,

Update: I'm reviewing all bingings to collections of my program and I found an interesting scenario that might be the reason:

First I have a CollectionViewSource bound with an ObservableCollection

<CollectionViewSource x:Key="deviceViewSource" Source="{Binding ElementName=_this, Path=Devices}"
                          IsLiveGroupingRequested="True">
        <CollectionViewSource.GroupDescriptions>
            <PropertyGroupDescription Converter="{StaticResource deviceGroupConverter}" />
        </CollectionViewSource.GroupDescriptions>
    </CollectionViewSource>

As the Source is Syncrhonized it might not be a problem. Note that I set LiveGrouping to true

Now it's the interesting part:

<ItemsControl ItemsSource="{Binding Source={StaticResource deviceViewSource}, Path=Groups}"
    <ItemsControl.ItemTemplate>
            <DataTemplate>
                . . . 
                <ItemsControl ItemsSource="{Binding Items}" >
                      . . .
                </ItemsControl>
             </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

If Items or Groups of CollectionViewSource is not thread-safe, I might have a problem??

Update2: I confirmed that the problem is with CollectionViewSource and Groupping. Since I couldn't solve this problem, my solution was to manually group and sync the UI.

As suggested BindingOperations.EnableCollectionSynchronization might solve this issue but I haven't tried.

Anyway, it's weird that CollectionViewSource is not thread-safe, even tough I'm using an ObservableCollection as a Source.

1 Answers
Related