AvaloniaUI / Avalonia

Develop Desktop, Embedded, Mobile and WebAssembly apps with C# and XAML. The most popular .NET UI client technology
https://avaloniaui.net
MIT License
26.1k stars 2.26k forks source link

The type initializer for 'Avalonia.Controls.Window' threw an exception #15005

Open mysteryx93 opened 8 months ago

mysteryx93 commented 8 months ago

Describe the bug

Got this error while opening my published app on Linux. This is the first time I see this error; just updated to Plasma 6. Relaunched the app and it works.

App was published with .NET 8 and Avalonia 11.0.6

Has anyone seen this error before?

System.TypeInitializationException: The type initializer for 'Avalonia.Controls.ApplicationLifetimes.ClassicDesktopStyleApplicationLifetime' threw an exception.
 ---> System.TypeInitializationException: The type initializer for 'Avalonia.Controls.Window' threw an exception.
 ---> System.InvalidOperationException: Operations that change non-concurrent collections must have exclusive access. A concurrent update was performed on this collection and corrupted its state. The collection's state is no longer correct.
   at System.Collections.Generic.Dictionary`2.FindValue(TKey)
   at System.Collections.Generic.Dictionary`2.ContainsKey(TKey)
   at Avalonia.AvaloniaPropertyRegistry.Register(Type, AvaloniaProperty)
   at Avalonia.AvaloniaProperty.Register[TOwner,TValue](String, TValue , Boolean , BindingMode , Func`2 , Func`3 , Boolean )
   at Avalonia.Controls.Window..cctor()

To Reproduce

God knows

Expected behavior

App runs

Avalonia version

11.0.6

OS

Linux

Additional context

No response

maxkatz6 commented 8 months ago

When it's "God knows", it's often about threading. And this error message confirms it. Can it be that your app interacts with Avalonia APIs from multiple threads?

maxkatz6 commented 8 months ago

Specifically, AvaloniaPropertyRegistry.Register might have been executed in parallel from different static constructors, while being accidentally accessed from the thread pool thread. We probably should add Dispatcher.CheckAccess to the Register method https://github.com/AvaloniaUI/Avalonia/blob/master/src/Avalonia.Base/AvaloniaPropertyRegistry.cs, so it would fail earlier.

mysteryx93 commented 8 months ago

This kind of problem would be very hard to diagnose, especially since I only saw it once. Adding some fail-early checks would help make sure it doesn't happen again.

I've also had some people complain that the app wasn't working anymore in some Windows 11... haven't investigated that yet. But in that case it doesn't even launch to give an error message or anything. But that's a totally separate issue. Did you see any issue with Avalonia on Windows 11?

maxkatz6 commented 8 months ago

No, I don't think anything major was reported about Win11.

kekekeks commented 8 months ago

We probably should add Dispatcher.CheckAccess to the Register method https://github.com/AvaloniaUI/Avalonia/blob/master/src/Avalonia.Base/AvaloniaPropertyRegistry.cs, so it would fail earlier.

At some point we want to have proper support for multiple dispatchers to make things like HostVisual and VisualTarget to work

timunie commented 8 months ago

11.0.6 is pretty outdated imo. Does the same happen in 11.1.0-beta1?

mysteryx93 commented 8 months ago

I haven't re-published the app since. I'll probably re-publish it when 11.1 comes out.