Bug 59411 - KeyNotFoundException: The given key was not present in the dictionary.
Summary: KeyNotFoundException: The given key was not present in the dictionary.
Status: RESOLVED NOT_REPRODUCIBLE
Alias: None
Product: Forms
Classification: Xamarin
Component: Android ()
Version: 2.4.0
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Bugzilla
URL:
: 59519 ()
Depends on:
Blocks:
 
Reported: 2017-09-12 09:39 UTC by Dániel Buga
Modified: 2017-12-04 17:00 UTC (History)
3 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED NOT_REPRODUCIBLE

Description Dániel Buga 2017-09-12 09:39:16 UTC
Today I've encountered a bug that seems like a race condition. My app keeps crashing at startup randomly with stack traces similar to this one (but may or may not be exactly the same)

KeyNotFoundException: The given key was not present in the dictionary.

 at System.Collections.Generic.Dictionary`2[TKey,TValue].get_Item (TKey key) [0x0001e] in <896ad1d315ca4ba7b117efb8dacaedcf>:0 
  at Xamarin.Forms.Platform.Android.Platform+DefaultRenderer.GetChildDrawingOrder (System.Int32 childCount, System.Int32 i) [0x00070] in <71a3106472a0430fb37743bb13e044f9>:0 
  at Android.Views.ViewGroup.n_GetChildDrawingOrder_II (System.IntPtr jnienv, System.IntPtr native__this, System.Int32 childCount, System.Int32 i) [0x00008] in <d278c06ad5684d6882c743a94a93ebc2>:0 
  at (wrapper dynamic-method) System.Object:96716b2d-a301-40fc-a181-d8b578c9ab09 (intptr,intptr,int,int)
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <896ad1d315ca4ba7b117efb8dacaedcf>:0 
  at Java.Interop.JniEnvironment+InstanceMethods.CallNonvirtualBooleanMethod (Java.Interop.JniObjectReference instance, Java.Interop.JniObjectReference type, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x00089] in <6cd960837cc24c26bab2a0a29b597627>:0 
  at Java.Interop.JniPeerMembers+JniInstanceMethods.InvokeVirtualBooleanMethod (System.String encodedMember, Java.Interop.IJavaPeerable self, Java.Interop.JniArgumentValue* parameters) [0x0005d] in <6cd960837cc24c26bab2a0a29b597627>:0 
  at Android.Views.View.DispatchTouchEvent (Android.Views.MotionEvent e) [0x00031] in <d278c06ad5684d6882c743a94a93ebc2>:0 
  at Xamarin.Forms.Platform.Android.Platform+DefaultRenderer.DispatchTouchEvent (Android.Views.MotionEvent e) [0x00007] in <71a3106472a0430fb37743bb13e044f9>:0 
  at Android.Views.View.n_DispatchTouchEvent_Landroid_view_MotionEvent_ (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_e) [0x0000f] in <d278c06ad5684d6882c743a94a93ebc2>:0 
  at (wrapper dynamic-method) System.Object:1db5c94a-6ac6-42a0-bf9a-bbd07d3e8e79 (intptr,intptr,intptr)
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <896ad1d315ca4ba7b117efb8dacaedcf>:0 
  at Java.Interop.JniEnvironment+InstanceMethods.CallNonvirtualBooleanMethod (Java.Interop.JniObjectReference instance, Java.Interop.JniObjectReference type, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x00089] in <6cd960837cc24c26bab2a0a29b597627>:0 
  at Java.Interop.JniPeerMembers+JniInstanceMethods.InvokeVirtualBooleanMethod (System.String encodedMember, Java.Interop.IJavaPeerable self, Java.Interop.JniArgumentValue* parameters) [0x0005d] in <6cd960837cc24c26bab2a0a29b597627>:0 
  at Android.Views.View.DispatchTouchEvent (Android.Views.MotionEvent e) [0x00031] in <d278c06ad5684d6882c743a94a93ebc2>:0 
  at Xamarin.Forms.Platform.Android.Platform+DefaultRenderer.DispatchTouchEvent (Android.Views.MotionEvent e) [0x00007] in <71a3106472a0430fb37743bb13e044f9>:0 
  at Android.Views.View.n_DispatchTouchEvent_Landroid_view_MotionEvent_ (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_e) [0x0000f] in <d278c06ad5684d6882c743a94a93ebc2>:0 
  at (wrapper dynamic-method) System.Object:1db5c94a-6ac6-42a0-bf9a-bbd07d3e8e79 (intptr,intptr,intptr)

Checking the source, the only way this can happen is that Platform.DefaultRenderer.InvalidateMinimumElevation() gets called while executing Platform.DefaultRenderer.GetChildDrawingOrder so _minimumElevation in the loop is changed while executing that loop.

This issue appeared after I refactored my apps navigation to a master-detail page with a listview in it, but the app crashes before the listview could be touched... Because this is an indeterministic crash, I cannot supply a repro project.
Comment 1 Paul DiPietro [MSFT] 2017-09-14 03:53:28 UTC
How frequently would you say this crash is occurring, and has it occurred on versions prior to the 2.4.0 prereleases (just so we know if this is any kind of regression)? I know you said you're unable to supply a repro project, but even one that exhibits the behavior infrequently is certainly better than no reproduction at all, if only to also get an idea of what your app looks like.
Comment 2 Dániel Buga 2017-09-14 08:16:24 UTC
With some luck I can repro with mashing on my screen furiously while the app is starting up. May or may not be my fault, will test on some demo apps if my time allows.
Comment 3 Paul DiPietro [MSFT] 2017-09-15 14:12:27 UTC
*** Bug 59519 has been marked as a duplicate of this bug. ***
Comment 4 Namyslaw Szymaniuk 2017-09-15 14:15:25 UTC
FYI - ticket https://bugzilla.xamarin.com/show_bug.cgi?id=59519 contains more detailed stacktrace, which tells exactly which LoC is affected - *Xamarin.Forms.Platform.Android\Renderers\ListViewAdapter.cs:111*
Comment 5 Dániel Buga 2017-09-15 15:07:27 UTC
That means I don't have to point at the countless async void methods I have. Good.

One more regarding DispatchTouchEvent:

KeyNotFoundException: The given key was not present in the dictionary.
  at (wrapper managed-to-native) Java.Interop.NativeMethods:java_interop_jnienv_call_nonvirtual_boolean_method_a (intptr,intptr&,intptr,intptr,intptr,Java.Interop.JniArgumentValue*)
  at Java.Interop.JniEnvironment+InstanceMethods.CallNonvirtualBooleanMethod (Java.Interop.JniObjectReference instance, Java.Interop.JniObjectReference type, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x00073] in <6cd960837cc24c26bab2a0a29b597627>:0 
  at Java.Interop.JniPeerMembers+JniInstanceMethods.InvokeVirtualBooleanMethod (System.String encodedMember, Java.Interop.IJavaPeerable self, Java.Interop.JniArgumentValue* parameters) [0x0005d] in <6cd960837cc24c26bab2a0a29b597627>:0 
  at Android.Views.View.DispatchTouchEvent (Android.Views.MotionEvent e) [0x00031] in <d278c06ad5684d6882c743a94a93ebc2>:0 
  at Xamarin.Forms.Platform.Android.Platform+DefaultRenderer.DispatchTouchEvent (Android.Views.MotionEvent e) [0x00000] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\Renderers\ListViewAdapter.cs:99 
  at Android.Views.View.n_DispatchTouchEvent_Landroid_view_MotionEvent_ (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_e) [0x0000f] in <d278c06ad5684d6882c743a94a93ebc2>:0 
  at (wrapper dynamic-method) System.Object:66353dae-5f33-4cb2-a97e-d0418b84b429 (intptr,intptr,intptr)
--- End of stack trace from previous location where exception was thrown ---
  at (wrapper managed-to-native) Java.Interop.NativeMethods:java_interop_jnienv_call_nonvirtual_boolean_method_a (intptr,intptr&,intptr,intptr,intptr,Java.Interop.JniArgumentValue*)
  at Java.Interop.JniEnvironment+InstanceMethods.CallNonvirtualBooleanMethod (Java.Interop.JniObjectReference instance, Java.Interop.JniObjectReference type, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x00073] in <6cd960837cc24c26bab2a0a29b597627>:0 
  at Java.Interop.JniPeerMembers+JniInstanceMethods.InvokeVirtualBooleanMethod (System.String encodedMember, Java.Interop.IJavaPeerable self, Java.Interop.JniArgumentValue* parameters) [0x0005d] in <6cd960837cc24c26bab2a0a29b597627>:0 
  at Android.Views.View.DispatchTouchEvent (Android.Views.MotionEvent e) [0x00031] in <d278c06ad5684d6882c743a94a93ebc2>:0 
  at Xamarin.Forms.Platform.Android.Platform+DefaultRenderer.DispatchTouchEvent (Android.Views.MotionEvent e) [0x00000] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\Renderers\ListViewAdapter.cs:99 
  at Android.Views.View.n_DispatchTouchEvent_Landroid_view_MotionEvent_ (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_e) [0x0000f] in <d278c06ad5684d6882c743a94a93ebc2>:0 
  at (wrapper dynamic-method) System.Object:66353dae-5f33-4cb2-a97e-d0418b84b429 (intptr,intptr,intptr)

My local crash reporting tool has 6 different stack traces, but is not able to differentiate between ones that are the same but thrown deeper, so I assumed all were the same and just noticed this one with line no. 99 in it (\Xamarin.Forms.Platform.Android\Renderers\ListViewAdapter.cs:99 )

Don't know if it helps or just makes things more confusing.
Comment 6 Paul DiPietro [MSFT] 2017-12-04 17:00:27 UTC
For the time being, as we don't have any real reproduction to go off of and no further reports of this have been made, we can reasonably assume that this might no longer be an issue as of 2.5.0 stable. That said, please do reopen this if we can get a reproduction to work with if the issue still occurs.