xamarin / XamarinCommunityToolkit

The Xamarin Community Toolkit is a collection of Animations, Behaviors, Converters, and Effects for mobile development with Xamarin.Forms. It simplifies and demonstrates common developer tasks building iOS, Android, and UWP apps with Xamarin.Forms.
MIT License
1.58k stars 471 forks source link

[Bug] Occasional crash in TouchEffect because of wrong time zone #1999

Open holecekp opened 11 months ago

holecekp commented 11 months ago

Description

DateTime.Now sometimes throws exceptions on Android because of wrong time zone data. This has been reported in https://github.com/xamarin/xamarin-android/issues/8090. The bug report has been closed because it cannot be reporoduced and the cause has not been found. The working workaround is to use NowUtc instead of Now as a fall-back. This solved the problem in my code. However, I have recently received other crash reports from AppCenter where the same bug is in CommunityToolkit's TouchEffect.

Would it be please possible to replace occurences of DateTime.Now with DateTime.UtcNow in the TouchEffect constructor (https://github.com/xamarin/XamarinCommunityToolkit/blob/main/src/CommunityToolkit/Xamarin.CommunityToolkit/Effects/Touch/TouchEffect.shared.cs)? They are used there anyway only as a workaround for a linker issue.

Stack Trace

Xamarin Exception Stack:
System.TimeZoneNotFoundException: Exception of type 'System.TimeZoneNotFoundException' was thrown.
  at System.TimeZoneInfo.get_Local () [0x00012] in <cb53bf87fe9b44d48a7f960c04d83290>:0
  at System.TimeZoneInfo.GetDateTimeNowUtcOffsetFromUtc (System.DateTime time, System.Boolean& isAmbiguousLocalDst) [0x00000] in <cb53bf87fe9b44d48a7f960c04d83290>:0
  at System.DateTime.get_Now () [0x00008] in <cb53bf87fe9b44d48a7f960c04d83290>:0
  at Xamarin.CommunityToolkit.Effects.TouchEffect..ctor () [0x00021] in <a221ea4c383647198845c500162d6404>:0
  at Xamarin.CommunityToolkit.Effects.TouchEffect.TryGenerateEffect (Xamarin.Forms.BindableObject bindable, System.Object oldValue, System.Object newValue) [0x00023] in <a221ea4c383647198845c500162d6404>:0
  at Xamarin.Forms.BindableObject.SetValueActual (Xamarin.Forms.BindableProperty property, Xamarin.Forms.BindableObject+BindablePropertyContext context, System.Object value, System.Boolean currentlyApplying, Xamarin.Forms.Internals.SetValueFlags attributes, System.Boolean silent) [0x0012a] in <fdb98549974543d69e8151b63f809e44>:0
  at Xamarin.Forms.BindableObject.SetValueCore (Xamarin.Forms.BindableProperty property, System.Object value, Xamarin.Forms.Internals.SetValueFlags attributes, Xamarin.Forms.BindableObject+SetValuePrivateFlags privateAttributes) [0x00173] in <fdb98549974543d69e8151b63f809e44>:0
  at Xamarin.Forms.BindingExpression.ApplyCore (System.Object sourceObject, Xamarin.Forms.BindableObject target, Xamarin.Forms.BindableProperty property, System.Boolean fromTarget) [0x00226] in <fdb98549974543d69e8151b63f809e44>:0
  at Xamarin.Forms.BindingExpression.Apply (System.Object sourceObject, Xamarin.Forms.BindableObject target, Xamarin.Forms.BindableProperty property) [0x0006b] in <fdb98549974543d69e8151b63f809e44>:0
  at Xamarin.Forms.Binding.Apply (System.Object context, Xamarin.Forms.BindableObject bindObj, Xamarin.Forms.BindableProperty targetProperty, System.Boolean fromBindingContextChanged) [0x0006d] in <fdb98549974543d69e8151b63f809e44>:0
  at Xamarin.Forms.BindableObject.SetBinding (Xamarin.Forms.BindableProperty targetProperty, Xamarin.Forms.BindingBase binding, System.Boolean fromStyle) [0x00091] in <fdb98549974543d69e8151b63f809e44>:0
  at Xamarin.Forms.BindableObject.SetBinding (Xamarin.Forms.BindableProperty targetProperty, Xamarin.Forms.BindingBase binding) [0x00000] in <fdb98549974543d69e8151b63f809e44>:0
  at MorseCode.Views.MorseCodesListPage+<InitializeComponent>_anonXamlCDataTemplate_11.LoadDataTemplate () [0x03230] in <0bdd02de54714e31b37dedfc795b4e9c>:0
  at Xamarin.Forms.ElementTemplate.CreateContent () [0x0002c] in <fdb98549974543d69e8151b63f809e44>:0
  at Xamarin.Forms.Platform.Android.TemplatedItemViewHolder.Bind (System.Object itemBindingContext, Xamarin.Forms.ItemsView itemsView, System.Action`1[T] reportMeasure, System.Nullable`1[T] size) [0x00028] in <1a87a07025014f35b0adfa51028d0901>:0
  at Xamarin.Forms.Platform.Android.ItemsViewAdapter`2[TItemsView,TItemsViewSource].BindTemplatedItemViewHolder (Xamarin.Forms.Platform.Android.TemplatedItemViewHolder templatedItemViewHolder, System.Object context) [0x00000] in <1a87a07025014f35b0adfa51028d0901>:0
  at Xamarin.Forms.Platform.Android.StructuredItemsViewAdapter`2[TItemsView,TItemsViewSource].BindTemplatedItemViewHolder (Xamarin.Forms.Platform.Android.TemplatedItemViewHolder templatedItemViewHolder, System.Object context) [0x00038] in <1a87a07025014f35b0adfa51028d0901>:0
  at Xamarin.Forms.Platform.Android.ItemsViewAdapter`2[TItemsView,TItemsViewSource].OnBindViewHolder (AndroidX.RecyclerView.Widget.RecyclerView+ViewHolder holder, System.Int32 position) [0x0004c] in <1a87a07025014f35b0adfa51028d0901>:0
  at Xamarin.Forms.Platform.Android.StructuredItemsViewAdapter`2[TItemsView,TItemsViewSource].OnBindViewHolder (AndroidX.RecyclerView.Widget.RecyclerView+ViewHolder holder, System.Int32 position) [0x00056] in <1a87a07025014f35b0adfa51028d0901>:0
  at Xamarin.Forms.Platform.Android.SelectableItemsViewAdapter`2[TItemsView,TItemsSource].OnBindViewHolder (AndroidX.RecyclerView.Widget.RecyclerView+ViewHolder holder, System.Int32 position) [0x00000] in <1a87a07025014f35b0adfa51028d0901>:0
  at Xamarin.Forms.Platform.Android.GroupableItemsViewAdapter`2[TItemsView,TItemsViewSource].OnBindViewHolder (AndroidX.RecyclerView.Widget.RecyclerView+ViewHolder holder, System.Int32 position) [0x0004b] in <1a87a07025014f35b0adfa51028d0901>:0
  at AndroidX.RecyclerView.Widget.RecyclerView+Adapter.n_OnBindViewHolder_Landroidx_recyclerview_widget_RecyclerView_ViewHolder_I (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_holder, System.Int32 position) [0x0000f] in <9f62edf4a26b41938f79cddc40df3c5c>:0
  at (wrapper delegate-invoke) <Module>.invoke_void_intptr_intptr_intptr_int(intptr,intptr,intptr,int)
  at Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPLI_V (_JniMarshal_PPLI_V callback, System.IntPtr jnienv, System.IntPtr klazz, System.IntPtr p0, System.Int32 p1) [0x00005] in <96af022141734842977c0f57b7524f48>:0

Link to Reproduction Sample

Cannot be reproduced (see below).

Steps to Reproduce

Unable to reproduce. The problem is only in certain time zones and probably only on certain devices there.

Expected Behavior

Random crashes on specific devices in certain time zones on Android.

Actual Behavior

No crashes

Basic Information

Workaround

The only workaround would be to copy all the TouchEffect's code into your solution and replace all calls of DateTime.Now with UtcNow there. More user friendly would be to fix it in the CommunityToolkit iteslf.