dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
14.17k stars 4.42k forks source link

[mono][ios] `ToFrozenSet` crashes on iOS devices with `8.0.3` release #101427

Open ivanpovazan opened 1 week ago

ivanpovazan commented 1 week ago

Description

With 8.0.3+ servicing release if an iOS app uses ToFrozenSet directly (or through some dependency CommunityToolkit) the app crashes with something like:

at System.Collections.Frozen.SmallFrozenSet`1.GSW[[UIKit.UIView, Microsoft.iOS, Version=17.2.0.0, Culture=neutral, PublicKeyToken=84e04ff9cfb79065]].Store(FrozenSet`1 )
   at System.Collections.Frozen.FrozenSetInternalBase`2[[UIKit.UIView, Microsoft.iOS, Version=17.2.0.0, Culture=neutral, PublicKeyToken=84e04ff9cfb79065],[System.Collections.Frozen.SmallFrozenSet`1.GSW[[UIKit.UIView, Microsoft.iOS, Version=17.2.0.0, Culture=neutral, PublicKeyToken=84e04ff9cfb79065]], System.Collections.Immutable, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a]]..ctor(IEqualityComparer`1 )
   at System.Collections.Frozen.SmallFrozenSet`1[[UIKit.UIView, Microsoft.iOS, Version=17.2.0.0, Culture=neutral, PublicKeyToken=84e04ff9cfb79065]]..ctor(HashSet`1 )
   at System.Collections.Frozen.FrozenSet.CreateFromSet[UIView](HashSet`1 )
   at System.Collections.Frozen.FrozenSet.ToFrozenSet[UIView](IEnumerable`1 , IEqualityComparer`1 )
   at CommunityToolkit.Maui.Core.Views.AlertView.get_Children()
   at CommunityToolkit.Maui.Core.Views.AlertView.Initialize()
   at CommunityToolkit.Maui.Core.Views.Alert.Show()
   at CommunityToolkit.Maui.Alerts.Toast.ShowPlatform(CancellationToken token)
   at CommunityToolkit.Maui.Alerts.Toast.Show(CancellationToken token)
   at KinesiaOne.ViewModels.SettingsPageViewModel.VersionTapped()

Regression

Yes, this is a regression compared to 8.0.2 servicing release, and is caused by this backport: https://github.com/dotnet/runtime/pull/97850

Analysis

The problem seems to be related to the fact that the problematic backport actually needs https://github.com/dotnet/runtime/pull/94787 also to be backported in order to work properly. @kotlarmilos could you issue the backport?

This is not reproducible on the main (.NET9) branch because we have the both changes (linked above) merged in.

Known workarounds

Reported as issue

Originally reported in various places: https://github.com/CommunityToolkit/Maui/issues/1752 https://github.com/CommunityToolkit/Maui/issues/1768 https://github.com/MudBlazor/MudBlazor/issues/6558

dotnet-policy-service[bot] commented 1 week ago

Tagging subscribers to 'os-ios': @steveisok, @akoeplinger, @kotlarmilos See info in area-owners.md if you want to be subscribed.

ivanpovazan commented 1 week ago

Smaller repro with a full AOTed console app (that crashes with 8.0.3 release):

using System;
using System.Collections.Generic;
using System.Collections.Frozen;

namespace HelloWorld
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            try
            {
                var people = new List<Person>()
                { 
                    new Person("John") 
                };

                foreach (var frozenPerson in people.ToFrozenSet())
                {
                    Console.WriteLine($"Element in frozen set has: {frozenPerson.Name} name");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }
    }

    class Person
    {
        public Person(string name) => Name = name;
        public string Name { get; set; }
    }
}
jkotas commented 1 week ago

FWIW, CommunityToolkit should not be using FrozenSet in the first place. I have filled https://github.com/CommunityToolkit/Maui/issues/1829 on it.

plppp2001 commented 1 week ago

FWIW, CommunityToolkit should not be using FrozenSet in the first place. I have filled https://github.com/CommunityToolkit/Maui/issues/1829 on it.

Yeah exactly. Agreed.