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
25.76k stars 2.23k forks source link

Possibly broken for Linux on Mono (Segfault) #5515

Closed andrfgs closed 3 years ago

andrfgs commented 3 years ago

Describe the bug Avalonia segfaults on Linux when using mono. I am using SkiaSharp (2.80.3-preview.40) and HarfBuzzSharp (2.6.1.7) through Nuget along with their SkiaSharp.NativeAssets.Linux and HarfBuzzSharp.NativeAssets.Linux respectively. For some reason, I need to manually place the .so files on the bin folder, but that's not really an issue. The real issue is Avalonia segfaults upon launch.

To Reproduce

  1. Build the project
  2. Run it using /usr/bin/mono-sgen /home/andre/RiderProjects/AvaloniaApp/AvaloniaApp/bin/Debug/net461/AvaloniaApp.exe

Expected behavior The program should launch the Avalonia app.

Native Crash Report Upon running: /usr/bin/mono-sgen /home/andre/RiderProjects/AvaloniaApp/AvaloniaApp/bin/Debug/net461/AvaloniaApp.exe

The following crash happens:

=================================================================
        Native Crash Reporting
=================================================================
Got a SIGSEGV while executing native code. This usually indicates
a fatal error in the mono runtime or one of the native libraries 
used by your application.
=================================================================

=================================================================
        Native stacktrace:
=================================================================
        0x55bf79a49ffd - /usr/bin/mono-sgen : 
        0x55bf79a4a3a9 - /usr/bin/mono-sgen : 
        0x55bf799f49e9 - /usr/bin/mono-sgen : 
        0x55bf79a436b9 - /usr/bin/mono-sgen : 
        0x55bf7d267490 - Unknown

=================================================================
        Telemetry Dumper:
=================================================================
Pkilling 0x140179027424832x from 0x140179075221312x
Pkilling 0x140179015726656x from 0x140179075221312x
Pkilling 0x140178590443072x from 0x140179075221312x
Entering thread summarizer pause from 0x140179075221312x
Finished thread summarizer pause from 0x140179075221312x.
Failed to create breadcrumb file (null)/crash_hash_0xfffd5223a2cef5ac

Waiting for dumping threads to resume

=================================================================
        External Debugger Dump:
=================================================================
[New LWP 81233]
[New LWP 81234]
[New LWP 81235]
[New LWP 81250]
[New LWP 81251]
[New LWP 81252]
[New LWP 81253]
[New LWP 81254]
warning: File "/usr/bin/mono-sgen-gdb.py" auto-loading has been declined by your `auto-load safe-path' set to "$debugdir:$datadir/auto-load".
To enable execution of this file add
        add-auto-load-safe-path /usr/bin/mono-sgen-gdb.py
line to your configuration file "/home/andre/.gdbinit".
To completely disable this security protection add
        set auto-load safe-path /
line to your configuration file "/home/andre/.gdbinit".
For more information about this security protection see the
"Auto-loading safe path" section in the GDB manual.  E.g., run from the shell:
        info "(gdb)Auto-loading safe path"
Error while reading shared library symbols for /usr/lib/libpthread.so.0:
Cannot find user-level thread for LWP 81254: generic error
0x00007f7dfc08b14f in wait4 () from /usr/lib/libc.so.6
  Id   Target Id                   Frame 
* 1    LWP 81223 "mono-sgen"       0x00007f7dfc08b14f in wait4 () from /usr/lib/libc.so.6
  2    LWP 81233 "SGen worker"     0x00007f7dfc1bd9ba in __futex_abstimed_wait_common64 () from /usr/lib/libpthread.so.0
  3    LWP 81234 "Finalizer"       0x00007f7dfc1bd9ba in __futex_abstimed_wait_common64 () from /usr/lib/libpthread.so.0
  4    LWP 81235 "Timer-Scheduler" 0x00007f7dfc1bd9ba in __futex_abstimed_wait_common64 () from /usr/lib/libpthread.so.0
  5    LWP 81250 "mono-sg:disk$0"  0x00007f7dfc1bd9ba in __futex_abstimed_wait_common64 () from /usr/lib/libpthread.so.0
  6    LWP 81251 "mono-sg:disk$1"  0x00007f7dfc1bd9ba in __futex_abstimed_wait_common64 () from /usr/lib/libpthread.so.0
  7    LWP 81252 "mono-sg:disk$2"  0x00007f7dfc1bd9ba in __futex_abstimed_wait_common64 () from /usr/lib/libpthread.so.0
  8    LWP 81253 "mono-sg:disk$3"  0x00007f7dfc1bd9ba in __futex_abstimed_wait_common64 () from /usr/lib/libpthread.so.0
  9    LWP 81254 "mono-sgen"       0x00007f7dfc1bd9ba in __futex_abstimed_wait_common64 () from /usr/lib/libpthread.so.0

Thread 9 (LWP 81254 "mono-sgen"):
#0  0x00007f7dfc1bd9ba in __futex_abstimed_wait_common64 () from /usr/lib/libpthread.so.0
#1  0x00007f7dfc1b7574 in pthread_cond_timedwait@@GLIBC_2.3.2 () from /usr/lib/libpthread.so.0
#2  0x000055bf79cb536c in ?? ()
#3  0x000055bf79cc0845 in ?? ()
#4  0x000055bf79bfe821 in ?? ()
#5  0x000055bf79b8aaee in ?? ()
#6  0x000000004102bc7d in ?? ()
#7  0x00007f7dfb94cb58 in ?? ()
#8  0x00007f7dfb94ca58 in ?? ()
#9  0x00007f7ddf1699a0 in ?? ()
#10 0x00007f7dfb8412a8 in ?? ()
#11 0x0000000000000010 in ?? ()
#12 0x00007f7dc8002690 in ?? ()
#13 0x00007f7ddf169a20 in ?? ()
#14 0x00007f7ddf169900 in ?? ()
#15 0x00007f7dfb8412a8 in ?? ()
#16 0x00007f7df943d7bb in ?? () from /usr/lib/mono/4.5/mscorlib.dll.so
#17 0x0000000000000010 in ?? ()
#18 0x00007f7ddf169978 in ?? ()
#19 0x0000000001a2ae2c in ?? ()
#20 0x00007f7df943d84e in ?? () from /usr/lib/mono/4.5/mscorlib.dll.so
#21 0x00007f7dfb841270 in ?? ()
#22 0x0000000000029806 in ?? ()
#23 0x0000000001a2ae2c in ?? ()
#24 0x000000004102b79b in ?? ()
#25 0x0000000000000000 in ?? ()

Thread 8 (LWP 81253 "mono-sg:disk$3"):
#0  0x00007f7dfc1bd9ba in __futex_abstimed_wait_common64 () from /usr/lib/libpthread.so.0
#1  0x00007f7dfc1b7260 in pthread_cond_wait@@GLIBC_2.3.2 () from /usr/lib/libpthread.so.0
#2  0x00007f7df2925b3c in ?? () from /usr/lib/dri/iris_dri.so
#3  0x00007f7df2924308 in ?? () from /usr/lib/dri/iris_dri.so
#4  0x00007f7dfc1b1299 in start_thread () from /usr/lib/libpthread.so.0
#5  0x00007f7dfc0be153 in clone () from /usr/lib/libc.so.6

Thread 7 (LWP 81252 "mono-sg:disk$2"):
#0  0x00007f7dfc1bd9ba in __futex_abstimed_wait_common64 () from /usr/lib/libpthread.so.0
#1  0x00007f7dfc1b7260 in pthread_cond_wait@@GLIBC_2.3.2 () from /usr/lib/libpthread.so.0
#2  0x00007f7df2925b3c in ?? () from /usr/lib/dri/iris_dri.so
#3  0x00007f7df2924308 in ?? () from /usr/lib/dri/iris_dri.so
#4  0x00007f7dfc1b1299 in start_thread () from /usr/lib/libpthread.so.0
#5  0x00007f7dfc0be153 in clone () from /usr/lib/libc.so.6

Thread 6 (LWP 81251 "mono-sg:disk$1"):
#0  0x00007f7dfc1bd9ba in __futex_abstimed_wait_common64 () from /usr/lib/libpthread.so.0
#1  0x00007f7dfc1b7260 in pthread_cond_wait@@GLIBC_2.3.2 () from /usr/lib/libpthread.so.0
#2  0x00007f7df2925b3c in ?? () from /usr/lib/dri/iris_dri.so
#3  0x00007f7df2924308 in ?? () from /usr/lib/dri/iris_dri.so
#4  0x00007f7dfc1b1299 in start_thread () from /usr/lib/libpthread.so.0
#5  0x00007f7dfc0be153 in clone () from /usr/lib/libc.so.6

Thread 5 (LWP 81250 "mono-sg:disk$0"):
#0  0x00007f7dfc1bd9ba in __futex_abstimed_wait_common64 () from /usr/lib/libpthread.so.0
#1  0x00007f7dfc1b7260 in pthread_cond_wait@@GLIBC_2.3.2 () from /usr/lib/libpthread.so.0
#2  0x00007f7df2925b3c in ?? () from /usr/lib/dri/iris_dri.so
#3  0x00007f7df2924308 in ?? () from /usr/lib/dri/iris_dri.so
#4  0x00007f7dfc1b1299 in start_thread () from /usr/lib/libpthread.so.0
#5  0x00007f7dfc0be153 in clone () from /usr/lib/libc.so.6

Thread 4 (LWP 81235 "Timer-Scheduler"):
#0  0x00007f7dfc1bd9ba in __futex_abstimed_wait_common64 () from /usr/lib/libpthread.so.0
#1  0x00007f7dfc1b7574 in pthread_cond_timedwait@@GLIBC_2.3.2 () from /usr/lib/libpthread.so.0
#2  0x000055bf79cb536c in ?? ()
#3  0x000055bf79c0bc7d in ?? ()
#4  0x000055bf79c0d673 in ?? ()
#5  0x000055bf79c0df90 in ?? ()
#6  0x000055bf79bfe57b in ?? ()
#7  0x000055bf79b8b914 in ?? ()
#8  0x0000000040f8ba12 in ?? ()
#9  0x00007f7dfb8216a0 in ?? ()
#10 0x00007f7dfb821828 in ?? ()
#11 0x00007f7dfb821578 in ?? ()
#12 0x0000000000000000 in ?? ()

Thread 3 (LWP 81234 "Finalizer"):
#0  0x00007f7dfc1bd9ba in __futex_abstimed_wait_common64 () from /usr/lib/libpthread.so.0
#1  0x00007f7dfc1b9b98 in __new_sem_wait_slow64.constprop.0 () from /usr/lib/libpthread.so.0
#2  0x000055bf79c463e2 in ?? ()
#3  0x000055bf79bfceca in ?? ()
#4  0x00007f7dfc1b1299 in start_thread () from /usr/lib/libpthread.so.0
#5  0x00007f7dfc0be153 in clone () from /usr/lib/libc.so.6

Thread 2 (LWP 81233 "SGen worker"):
#0  0x00007f7dfc1bd9ba in __futex_abstimed_wait_common64 () from /usr/lib/libpthread.so.0
#1  0x00007f7dfc1b7260 in pthread_cond_wait@@GLIBC_2.3.2 () from /usr/lib/libpthread.so.0
#2  0x000055bf79ca6ed4 in ?? ()
#3  0x00007f7dfc1b1299 in start_thread () from /usr/lib/libpthread.so.0
#4  0x00007f7dfc0be153 in clone () from /usr/lib/libc.so.6

Thread 1 (LWP 81223 "mono-sgen"):
#0  0x00007f7dfc08b14f in wait4 () from /usr/lib/libc.so.6
#1  0x000055bf79a4a27d in ?? ()
#2  0x000055bf79a4a3a9 in ?? ()
#3  0x000055bf799f49e9 in ?? ()
#4  0x000055bf79a436b9 in ?? ()
#5  0x000055bf7d267490 in ?? ()
#6  0x00007f7de4240200 in ?? () from /usr/lib/libharfbuzz.so.0
#7  0x00007f7de422488c in hb_font_set_funcs () from /usr/lib/libharfbuzz.so.0
#8  0x00007f7dde3df491 in hb_font_create () from /home/andre/RiderProjects/AvaloniaApp/AvaloniaApp/bin/Debug/net461/libHarfBuzzSharp.so
#9  0x0000000040459413 in ?? ()
#10 0x00007f7dfb863f00 in ?? ()
#11 0x00007f7dfb863f00 in ?? ()
#12 0x00007f7dfb866510 in ?? ()
#13 0x00007f7dfb867288 in ?? ()
#14 0x00007f7dfb8665e0 in ?? ()
#15 0x000055bf79bfe2e0 in ?? ()
#16 0x0000000040459380 in ?? ()
#17 0x000055bf7b8758a0 in ?? ()
#18 0x00007ffeff41d350 in ?? ()
#19 0x00007ffeff41d010 in ?? ()
#20 0x00007f7dfb867288 in ?? ()
#21 0x000055bf7b84e720 in ?? ()
#22 0x00007ffeff41d350 in ?? ()
#23 0x00000000404592e8 in ?? ()
#24 0x00007f7dfb866510 in ?? ()
#25 0x00007f7dfb863f00 in ?? ()
#26 0x00007ffeff41d350 in ?? ()
#27 0x0000000040459290 in ?? ()
#28 0x00007ffeff41d350 in ?? ()
#29 0x0000000040456dd4 in ?? ()
#30 0x00007f7dfb85d9e8 in ?? ()
#31 0x0000000000000001 in ?? ()
#32 0x00007f7dfb85dac0 in ?? ()
#33 0x00007f7dde950ac0 in ?? ()
#34 0x00007ffeff41d2b0 in ?? ()
#35 0x00007ffeff41d0e8 in ?? ()
#36 0x000055bf7b87dd40 in ?? ()
#37 0x00007f7dfb866510 in ?? ()
#38 0x0000000000000000 in ?? ()
[Inferior 1 (process 81223) detached]

=================================================================
        Basic Fault Address Reporting
=================================================================
Memory around native instruction pointer (0x55bf7d267490):0x55bf7d267480  00 00 00 00 00 00 00 00 a1 00 00 00 00 00 00 00  ................
0x55bf7d267490  01 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00  ................
0x55bf7d2674a0  20 d9 6b de 7d 7f 00 00 90 10 55 7d bf 55 00 00   .k.}.....U}.U..
0x55bf7d2674b0  00 0b 00 00 00 0b 00 00 00 00 01 00 00 00 00 00  ................

=================================================================
        Managed Stacktrace:
=================================================================
          at <unknown> <0xffffffff>
          at HarfBuzzSharp.HarfBuzzApi:hb_font_create <0x00092>
          at HarfBuzzSharp.Font:.ctor <0x00057>
          at Avalonia.Skia.GlyphTypefaceImpl:.ctor <0x002c3>
          at Avalonia.Skia.FontManagerImpl:CreateGlyphTypeface <0x002e7>
          at Avalonia.Media.GlyphTypeface:.ctor <0x00069>
          at Avalonia.Media.FontManager:GetOrAddGlyphTypeface <0x000e3>
          at Avalonia.Media.Typeface:get_GlyphTypeface <0x00053>
          at Avalonia.Media.TextFormatting.TextCharacters:TryGetRunProperties <0x0014f>
          at Avalonia.Media.TextFormatting.TextCharacters:CreateShapeableRun <0x001b3>
          at Avalonia.Media.TextFormatting.TextCharacters:GetShapeableCharacters <0x00117>
          at Avalonia.Media.TextFormatting.TextFormatterImpl:FetchTextRuns <0x00577>
          at Avalonia.Media.TextFormatting.TextFormatterImpl:FormatLine <0x00087>
          at Avalonia.Media.TextFormatting.TextLayout:UpdateLayout <0x00583>
          at Avalonia.Media.TextFormatting.TextLayout:.ctor <0x0025b>
          at Avalonia.Controls.TextBlock:CreateTextLayout <0x0022b>
          at Avalonia.Controls.TextBlock:get_TextLayout <0x0006f>
          at Avalonia.Controls.TextBlock:MeasureOverride <0x001d7>
          at Avalonia.Layout.Layoutable:MeasureCore <0x0013f>
          at Avalonia.Layout.Layoutable:Measure <0x00284>
          at Avalonia.Layout.LayoutHelper:MeasureChild <0x000d2>
          at Avalonia.Layout.LayoutHelper:MeasureChild <0x000c3>
          at Avalonia.Controls.Presenters.ContentPresenter:MeasureOverride <0x000c3>
          at Avalonia.Layout.Layoutable:MeasureCore <0x0013f>
          at Avalonia.Layout.Layoutable:Measure <0x00284>
          at Avalonia.Layout.LayoutHelper:MeasureChild <0x000d2>
          at Avalonia.Controls.Decorator:MeasureOverride <0x0008f>
          at Avalonia.Controls.Primitives.VisualLayerManager:MeasureOverride <0x0012b>
          at Avalonia.Layout.Layoutable:MeasureCore <0x0013f>
          at Avalonia.Layout.Layoutable:Measure <0x00284>
          at Avalonia.Layout.Layoutable:MeasureOverride <0x0016a>
          at Avalonia.Layout.Layoutable:MeasureCore <0x0013f>
          at Avalonia.Layout.Layoutable:Measure <0x00284>
          at Avalonia.Layout.Layoutable:MeasureOverride <0x0016a>
          at Avalonia.Controls.Window:MeasureOverride <0x0034b>
          at Avalonia.Controls.WindowBase:MeasureCore <0x0007a>
          at Avalonia.Layout.Layoutable:Measure <0x00284>
          at Avalonia.Layout.LayoutManager:Measure <0x001ea>
          at Avalonia.Layout.LayoutManager:ExecuteInitialLayoutPass <0x0004b>
          at Avalonia.Controls.Window:ShowCore <0x005f2>
          at Avalonia.Controls.Window:Show <0x0002b>
          at Avalonia.Controls.ApplicationLifetimes.ClassicDesktopStyleApplicationLifetime:Start <0x000fb>
          at Avalonia.ClassicDesktopStyleApplicationLifetimeExtensions:StartWithClassicDesktopLifetime <0x000ef>
          at AvaloniaApp.Program:Main <0x0003f>
          at <Module>:runtime_invoke_void_object <0x00091>
=================================================================

Process finished with exit code 134.

Desktop (please complete the following information):

Additional context Avalonia works on .Net Core 5 fine, but I guess that is quite unrelated as the build system is completely different. Might this be a bug with the current preview versions?

I can't downgrade it though because Avalonia 10.0 requires SkiaSharp > 80.2, so the 80.2 is incompatible.

kekekeks commented 3 years ago

Make sure that you are using matching versions of native libraries and their managed wrappers (SkiaSharp/HarfbuzzSharp)

andrfgs commented 3 years ago

The native libraries are provided by the nuget packages as well and they are indeed on the same version.

Those are the nuget packages for the native version I use: SkiaSharp.NativeAssets.Linux 2.80.3-preview.40 HarfBuzzSharp.NativeAssets.Linux 2.6.1.7

Isn't it just this I need to provide?

kekekeks commented 3 years ago

I've checked yesterday with our default template, Ubuntu 18.04 and Mono 6.12.0.90, seems to work OK (except for the required workaround for SkiaSharp's musl binaries)

https://github.com/kekekeks/AvaloniaMonoDemo

andrfgs commented 3 years ago

OK so I was able to use that demo and it runs fine. My guess Is that perhaps creating the project using its template from Avalonia .Net Core App and then downgrading from .Net 5 to .Net Framework 4.72 won't work.

Since I'm using Linux and there seems to be no way to build a template of Avalonia for Mono, I simply copy pasted that example project and renamed it to my project name and that works.

The question now is. Is the Avalonia .Net Core App able/supposed to work in lower .Net versions by simply downgrading the .Net version on the .csproj file?

kekekeks commented 3 years ago

Avalonia itself is targeting netstandard2.0, netcoreapp2.0 and net461 as minimum supported versions of target frameworks. Templates are always targeting the latest stable .NET, but can be downgraded if needed.

We do not, however, support building with legacy SDKs (e. g. MSBuild 4.5 or xbuild from Mono), .NET Core 2.0+ SDK or .NET 5+ SDK are required.

andrfgs commented 3 years ago

I was using .Net Sdk 5.0 but the template says Avalonia .NET Core App. I tried downgrading and it was complaining with System.DllNotFoundException: Unable to load library 'libSkiaSharp' even though the .so files were there, so I tried many things such as LD_PRELOAD etc.

I finally found out why the examples work and this didn't work. Just as you mentioned in a previous issue, you need to apply that fix to tell SkiaSharp not to use Musl. I also had to rebuild the project, because for some reason it wasn't linking properly to the correct version. I understand the fix, but still doesn't explain why it wasn't finding the dll file. So I'm guessing I somehow corrupted the bin directory by manually placing the .so files there.

In short, this is how to get it to work: 1) Generate the project using dotnet new avalonia.app 2) Downgrade the project either by changing the .csproj file or using an IDE, I personally used Jetbrains Rider 3) Edit the .csproj file to make Skia not use musl on a Glibc installation, by adding:

<Target Name="SkiaSharpRemoveMusl" BeforeTargets="BeforeBuild">
    <ItemGroup>
        <Content Remove="@(Content)" Condition="$([System.Text.RegularExpressions.Regex]::IsMatch('%(FullPath)', 'packages.*linux-musl.*so')) " />
    </ItemGroup>
</Target>

4) Rebuilding the proj

I'm assuming the original problem was a link error, but that is now fixed. Thanks for all the help as well!