Skip to content

fix(linux): only init DpiBootstrap on Windows - #20557

Merged
ramezgerges merged 2 commits into
unoplatform:masterfrom
ramezgerges:dpibootstrap_crash_linux
May 27, 2025
Merged

ramezgerges merged 2 commits into
unoplatform:masterfrom
ramezgerges:dpibootstrap_crash_linux

Conversation

@ramezgerges

Copy link
Copy Markdown
Contributor

GitHub Issue (If applicable): closes #20544

PR Type

What kind of change does this PR introduce?

What is the current behavior?

What is the new behavior?

PR Checklist

Please check if your PR fulfills the following requirements:

Other information

Internal Issue (If applicable):

@unodevops

Copy link
Copy Markdown
Contributor

@unodevops

Copy link
Copy Markdown
Contributor

@github-actions github-actions Bot added the area/automation Categorizes an issue or PR as relevant to project automation label May 26, 2025
@unodevops

Copy link
Copy Markdown
Contributor

@unodevops

Copy link
Copy Markdown
Contributor

@ramezgerges
ramezgerges merged commit 5a71df1 into unoplatform:master May 27, 2025
@ramezgerges
ramezgerges deleted the dpibootstrap_crash_linux branch May 27, 2025 12:28
@HakanL

HakanL commented May 27, 2025

Copy link
Copy Markdown

@ramezgerges I tried version 6.1.0-dev.177 and the issue is still there. I don't think just checking the result is enough, it fails to load the DLL which will throw an exception (I think @jeromelaban commented on that as well). I think you need to have a check for OS prior to attempting to make the call. I'm surprised your CI works, it crashes on my RPi arm64 system.

@ramezgerges

Copy link
Copy Markdown
Contributor Author

@HakanL The file was moved to an assembly that is not loaded except on Windows. If it works in CI and on my side locally, it would be really surprising if it doesn't on your end. Maybe 177 doesn't the change.

@jeromelaban

jeromelaban commented May 27, 2025 •

Copy link
Copy Markdown
Member

It's not included yet and will be in unoplatform/uno.templates#1528. You need track the builds from main using https://github.com/unoplatform/uno.templates/actions/workflows/ci.yml?query=branch%3Amain. There's a summary with the packages included in the SDK.

@HakanL

HakanL commented May 27, 2025

Copy link
Copy Markdown

@jeromelaban @ramezgerges Ah, sorry, my bad, I saw it in the master git history and I thought it was included in the updated Nuget package since it was uploaded right around the same time.

@HakanL

HakanL commented May 27, 2025 •

Copy link
Copy Markdown

@jeromelaban I know I've jumped the gun several times, I'm eager to try out the fixes, but it would be very helpful if your CI could post in the PR comments which Nuget dev build version where the merged PR would be available.

@jeromelaban

Copy link
Copy Markdown
Member

it would be very helpful if your CI could post in the PR comments which Nuget dev build version where the merged PR would be available.

I agree, we're working towards this, but not there yet.

@HakanL

HakanL commented May 27, 2025

Copy link
Copy Markdown

@ramezgerges I can confirm it's working now with Uno.Sdk version 6.1.0-dev.179. Again sorry for jumping the gun before and doubting the fix :)

@jeromelaban

Copy link
Copy Markdown
Member

@Mergifyio backport release/stable/6.0

@mergify

mergify Bot commented May 28, 2025 •

Copy link
Copy Markdown
Contributor

backport release/stable/6.0

✅ Backports have been created

Details

jeromelaban added a commit that referenced this pull request May 28, 2025
…6.0/pr-20557

fix(linux): only init DpiBootstrap on Windows (backport #20557)
DevTKSS pushed a commit to DevTKSS/uno that referenced this pull request May 31, 2025
…ash_linux

fix(linux): only init DpiBootstrap on Windows
jonpryor added a commit that referenced this pull request Nov 7, 2025
("Module initializers considered possibly harmful?")

This reverts commit 1003d8c.

Context: #20443
Context: #20544
Context: #20557
Context: unoplatform/uno.chefs#1709

[Module Initializers][0] are code that:

> …is executed at, or sometime before, first access to any static field
> or first invocation of any method defined in the module.

In a CoreCLR or MonoVM context, a module initializer is typically
executed shortly after the assembly is loaded.

In a NativeAOT context, module initializers are executed
[*at startup*][1].  All of them.  All at once (sequentially).

This is fine…until you have a module initializer which assumes the
operating system that it's executing on.

Enter PR #20443, which introduced a module initializer which assumes
Windows.

This promptly broke Linux (#20544), the fix for which (#20557) was to
"only init DpiBootstrap on Windows".

Which works…so long as you have `DpiBootstrap` in a separate assembly
which is loaded on-demand.

Enter `src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent`,
which has a `.UseWin32()` invocation.  When built for NativeAOT, the
`.UseWin32()` invocation (eventually) causes
`Uno.UI.Runtime.Skia.Win32.Support.dll` to be included in the app and
the `DpiBootstrap` module initializer executed during startup, which
promptly fails in a manner similar to #20544.

A workaround for this is to use a `#if` to exclude the `.UseWin32()`
invocation when building for non-Windows platforms.  This allows the
trimmer to remove the reference to
`Uno.UI.Runtime.Skia.Win32.Support.dll`, avoiding its module
initializer; this is the approach taken in early drafts of
unoplatform/uno.chefs#1709.

Unfortunately, this doesn't "scale": we don't want every app to wrap
`.UseWin32()` invocations with a `#if`.

"Partially revert" part of #20443, returning to an earlier approach
wherein the DPI code is done within `Win32Host`, avoiding the use of
module initializers.

Test this scenario by updating `run-netcore-mobile-template-tests.ps1`
to allow running a binary after the build and before the clean.

Update `src/SolutionTemplate/5.6/uno56netcurrent` to support a new
`--exit` command-line argument; when provided, `App.OnLaunched()` will
call `Application.Exit()`, causing the app to exit.  This allows us to
start the app and have it exit on CI.  If it crashes, then the exit
code will be a non-zero value, e.g.

	Executing: 5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/uno56netcurrent --exit

	Unhandled exception. System.DllNotFoundException: Unable to load shared library 'USER32.dll' or one of its dependencies. In order to help diagnose loading problems, consider setting the DYLD_PRINT_LIBRARIES environment variable:
	dlopen(/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/USER32.dll.dylib, 0x0001): tried: '/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/USER32.dll.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/USER32.dll.dylib' (no such file), '/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/USER32.dll.dylib' (no such file)
	dlopen(USER32.dll.dylib, 0x0001): tried: 'USER32.dll.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OSUSER32.dll.dylib' (no such file), '/usr/lib/USER32.dll.dylib' (no such file, not in dyld cache), 'USER32.dll.dylib' (no such file)
	dlopen(/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/libUSER32.dll.dylib, 0x0001): tried: '/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/libUSER32.dll.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/libUSER32.dll.dylib' (no such file), '/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/libUSER32.dll.dylib' (no such file)
	dlopen(libUSER32.dll.dylib, 0x0001): tried: 'libUSER32.dll.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OSlibUSER32.dll.dylib' (no such file), '/usr/lib/libUSER32.dll.dylib' (no such file, not in dyld cache), 'libUSER32.dll.dylib' (no such file)
	dlopen(/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/USER32.dll, 0x0001): tried: '/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/USER32.dll' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/USER32.dll' (no such file), '/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/USER32.dll' (no such file)
	dlopen(USER32.dll, 0x0001): tried: 'USER32.dll' (no such file), '/System/Volumes/Preboot/Cryptexes/OSUSER32.dll' (no such file), '/usr/lib/USER32.dll' (no such file, not in dyld cache), 'USER32.dll' (no such file)
	dlopen(/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/libUSER32.dll, 0x0001): tried: '/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/libUSER32.dll' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/libUSER32.dll' (no such file), '/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/libUSER32.dll' (no such file)
	dlopen(libUSER32.dll, 0x0001): tried: 'libUSER32.dll' (no such file), '/System/Volumes/Preboot/Cryptexes/OSlibUSER32.dll' (no such file), '/usr/lib/libUSER32.dll' (no such file, not in dyld cache), 'libUSER32.dll' (no such file)

	   at System.Runtime.InteropServices.NativeLibrary.LoadLibErrorTracker.Throw(String) + 0x46
	   at Internal.Runtime.CompilerHelpers.InteropHelpers.FixupModuleCell(InteropHelpers.ModuleFixupCell*) + 0x12c
	   at Internal.Runtime.CompilerHelpers.InteropHelpers.ResolvePInvokeSlow(InteropHelpers.MethodFixupCell*) + 0x35
	   at Windows.Win32.PInvoke.<SetProcessDpiAwarenessContext>g__LocalExternFunction|267_0(DPI_AWARENESS_CONTEXT) + 0x22
	   at Windows.Win32.PInvoke.SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT) + 0x1b
	   at DpiBootstrap.Init() + 0x10
	   at Internal.Runtime.CompilerHelpers.StartupCodeHelpers.RunInitializers(TypeManagerHandle, ReadyToRunSectionType) + 0x45
	   at Internal.Runtime.CompilerHelpers.StartupCodeHelpers.RunModuleInitializers() + 0x37
	…
	Exception: /Users/runner/work/1/s/build/test-scripts/run-netcore-mobile-template-tests.ps1:13
	Line |
	  13 |          throw "Exit code must be zero."
	     |          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	     | Exit code must be zero.

[0]: https://github.com/dotnet/runtime/blob/2e85cbda5839dc36705686fc4a3d6b50f2cef5a9/docs/design/specs/Ecma-335-Augments.md#module-initializer
[1]: https://github.com/dotnet/runtime/blob/b1cb4c502f1eec66227a31e705934fb67fc091f0/src/coreclr/nativeaot/Common/src/Internal/Runtime/CompilerHelpers/tartupCodeHelpers.cs#L163-L169%5D
[2]: https://github.com/unoplatform/uno.chefs/blob/af0a0c928337688c5ed3e87c3389a1cfdad46933/Chefs/Platforms/Desktop/Program.cs#L15
jonpryor added a commit that referenced this pull request Nov 7, 2025
("Module initializers considered possibly harmful?")

This reverts commit 1003d8c.

Context: #20443
Context: #20544
Context: #20557
Context: unoplatform/uno.chefs#1709

[Module Initializers][0] are code that:

> …is executed at, or sometime before, first access to any static field
> or first invocation of any method defined in the module.

In a CoreCLR or MonoVM context, a module initializer is typically
executed shortly after the assembly is loaded.

In a NativeAOT context, module initializers are executed
[*at startup*][1].  All of them.  All at once (sequentially).

This is fine…until you have a module initializer which assumes the
operating system that it's executing on.

Enter PR #20443, which introduced a module initializer which assumes
Windows.

This promptly broke Linux (#20544), the fix for which (#20557) was to
"only init DpiBootstrap on Windows".

Which works…so long as you have `DpiBootstrap` in a separate assembly
which is loaded on-demand.

Enter `src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent`,
which has a `.UseWin32()` invocation.  When built for NativeAOT, the
`.UseWin32()` invocation (eventually) causes
`Uno.UI.Runtime.Skia.Win32.Support.dll` to be included in the app and
the `DpiBootstrap` module initializer executed during startup, which
promptly fails in a manner similar to #20544.

A workaround for this is to use a `#if` to exclude the `.UseWin32()`
invocation when building for non-Windows platforms.  This allows the
trimmer to remove the reference to
`Uno.UI.Runtime.Skia.Win32.Support.dll`, avoiding its module
initializer; this is the approach taken in early drafts of
unoplatform/uno.chefs#1709.

Unfortunately, this doesn't "scale": we don't want every app to wrap
`.UseWin32()` invocations with a `#if`.

"Partially revert" part of #20443, returning to an earlier approach
wherein the DPI code is done within `Win32Host`, avoiding the use of
module initializers.

Test this scenario by updating `run-netcore-mobile-template-tests.ps1`
to allow running a binary after the build and before the clean.

Update `src/SolutionTemplate/5.6/uno56netcurrent` to support a new
`--exit` command-line argument; when provided, `App.OnLaunched()` will
call `Application.Exit()`, causing the app to exit.  This allows us to
start the app and have it exit on CI.  If it crashes, then the exit
code will be a non-zero value, e.g.

	Executing: 5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/uno56netcurrent --exit

	Unhandled exception. System.DllNotFoundException: Unable to load shared library 'USER32.dll' or one of its dependencies. In order to help diagnose loading problems, consider setting the DYLD_PRINT_LIBRARIES environment variable:
	dlopen(/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/USER32.dll.dylib, 0x0001): tried: '/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/USER32.dll.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/USER32.dll.dylib' (no such file), '/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/USER32.dll.dylib' (no such file)
	dlopen(USER32.dll.dylib, 0x0001): tried: 'USER32.dll.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OSUSER32.dll.dylib' (no such file), '/usr/lib/USER32.dll.dylib' (no such file, not in dyld cache), 'USER32.dll.dylib' (no such file)
	dlopen(/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/libUSER32.dll.dylib, 0x0001): tried: '/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/libUSER32.dll.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/libUSER32.dll.dylib' (no such file), '/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/libUSER32.dll.dylib' (no such file)
	dlopen(libUSER32.dll.dylib, 0x0001): tried: 'libUSER32.dll.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OSlibUSER32.dll.dylib' (no such file), '/usr/lib/libUSER32.dll.dylib' (no such file, not in dyld cache), 'libUSER32.dll.dylib' (no such file)
	dlopen(/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/USER32.dll, 0x0001): tried: '/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/USER32.dll' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/USER32.dll' (no such file), '/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/USER32.dll' (no such file)
	dlopen(USER32.dll, 0x0001): tried: 'USER32.dll' (no such file), '/System/Volumes/Preboot/Cryptexes/OSUSER32.dll' (no such file), '/usr/lib/USER32.dll' (no such file, not in dyld cache), 'USER32.dll' (no such file)
	dlopen(/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/libUSER32.dll, 0x0001): tried: '/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/libUSER32.dll' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/libUSER32.dll' (no such file), '/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/libUSER32.dll' (no such file)
	dlopen(libUSER32.dll, 0x0001): tried: 'libUSER32.dll' (no such file), '/System/Volumes/Preboot/Cryptexes/OSlibUSER32.dll' (no such file), '/usr/lib/libUSER32.dll' (no such file, not in dyld cache), 'libUSER32.dll' (no such file)

	   at System.Runtime.InteropServices.NativeLibrary.LoadLibErrorTracker.Throw(String) + 0x46
	   at Internal.Runtime.CompilerHelpers.InteropHelpers.FixupModuleCell(InteropHelpers.ModuleFixupCell*) + 0x12c
	   at Internal.Runtime.CompilerHelpers.InteropHelpers.ResolvePInvokeSlow(InteropHelpers.MethodFixupCell*) + 0x35
	   at Windows.Win32.PInvoke.<SetProcessDpiAwarenessContext>g__LocalExternFunction|267_0(DPI_AWARENESS_CONTEXT) + 0x22
	   at Windows.Win32.PInvoke.SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT) + 0x1b
	   at DpiBootstrap.Init() + 0x10
	   at Internal.Runtime.CompilerHelpers.StartupCodeHelpers.RunInitializers(TypeManagerHandle, ReadyToRunSectionType) + 0x45
	   at Internal.Runtime.CompilerHelpers.StartupCodeHelpers.RunModuleInitializers() + 0x37
	…
	Exception: /Users/runner/work/1/s/build/test-scripts/run-netcore-mobile-template-tests.ps1:13
	Line |
	  13 |          throw "Exit code must be zero."
	     |          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	     | Exit code must be zero.

[0]: https://github.com/dotnet/runtime/blob/2e85cbda5839dc36705686fc4a3d6b50f2cef5a9/docs/design/specs/Ecma-335-Augments.md#module-initializer
[1]: https://github.com/dotnet/runtime/blob/b1cb4c502f1eec66227a31e705934fb67fc091f0/src/coreclr/nativeaot/Common/src/Internal/Runtime/CompilerHelpers/tartupCodeHelpers.cs#L163-L169%5D
[2]: https://github.com/unoplatform/uno.chefs/blob/af0a0c928337688c5ed3e87c3389a1cfdad46933/Chefs/Platforms/Desktop/Program.cs#L15
jonpryor added a commit that referenced this pull request Nov 7, 2025
("Module initializers considered problematic?")

Context: #20443
Context: #20544
Context: #20557
Context: unoplatform/uno.chefs#1709

[Module Initializers][0] are code that:

> …is executed at, or sometime before, first access to any static field
> or first invocation of any method defined in the module.

In a CoreCLR or MonoVM context, a module initializer is typically
executed shortly after the assembly is loaded.

In a NativeAOT context, module initializers are executed
[*at startup*][1].  All of them.  All at once (sequentially).

This is fine…until you have a module initializer which assumes the
operating system that it's executing on.

Enter PR #20443, which introduced a module initializer which assumes
Windows.

This promptly broke Linux (#20544), the fix for which (#20557) was to
"only init DpiBootstrap on Windows".

Which works…so long as you have `DpiBootstrap` in a separate assembly
which is loaded on-demand.

Enter `src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent`,
which has a `.UseWin32()` invocation.  When run under NativeAOT:

	dotnet publish src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/*.csproj \
	  -c Release -r osx-x64 -p:UnoTargetFrameworkOverride=net10.0-desktop -f net10.0-desktop \
	  -p:SelfContained=true -p:PublishAot=true -bl

	src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/uno56netcurrent

the `.UseWin32()` invocation (eventually) causes
`Uno.UI.Runtime.Skia.Win32.Support.dll` to be included in the app and
the `DpiBootstrap` module initializer executed during startup, which
promptly fails in a manner similar to #20544:

	Unhandled exception. System.DllNotFoundException: Unable to load shared library 'USER32.dll' or one of its dependencies. In order to help diagnose loading problems, consider setting the DYLD_PRINT_LIBRARIES environment variable:
	…

A workaround for this is to use a `#if` to exclude the `.UseWin32()`
invocation when building for non-Windows platforms.  This allows the
trimmer to remove the reference to
`Uno.UI.Runtime.Skia.Win32.Support.dll`, avoiding its module
initializer; this is the approach taken in early drafts of
unoplatform/uno.chefs#1709.

Unfortunately, this doesn't "scale": we don't want every app to wrap
`.UseWin32()` invocations with a `#if`.

Update `DpiBootstrap` to wrap the `SetProcessDpiAwarenessContext()`
call in a `if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))`
block[^0].  This ensures the code is only run on Windows, and should
avoid the crash on non-Windows platforms.

Test this scenario by updating `run-netcore-mobile-template-tests.ps1`
to allow running a binary after the build and before the clean.

Update `src/SolutionTemplate/5.6/uno56netcurrent` to support a new
`--exit` command-line argument; when provided, `App.OnLaunched()` will
call `Application.Exit()`, causing the app to exit.  This allows us to
start the app and have it exit on CI.  If it crashes, then the exit
code will be a non-zero value, e.g.

	Executing: 5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/uno56netcurrent --exit

	Unhandled exception. System.DllNotFoundException: Unable to load shared library 'USER32.dll' or one of its dependencies. In order to help diagnose loading problems, consider setting the DYLD_PRINT_LIBRARIES environment variable:
	dlopen(/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/USER32.dll.dylib, 0x0001): tried: '/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/USER32.dll.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/USER32.dll.dylib' (no such file), '/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/USER32.dll.dylib' (no such file)
	dlopen(USER32.dll.dylib, 0x0001): tried: 'USER32.dll.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OSUSER32.dll.dylib' (no such file), '/usr/lib/USER32.dll.dylib' (no such file, not in dyld cache), 'USER32.dll.dylib' (no such file)
	dlopen(/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/libUSER32.dll.dylib, 0x0001): tried: '/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/libUSER32.dll.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/libUSER32.dll.dylib' (no such file), '/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/libUSER32.dll.dylib' (no such file)
	dlopen(libUSER32.dll.dylib, 0x0001): tried: 'libUSER32.dll.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OSlibUSER32.dll.dylib' (no such file), '/usr/lib/libUSER32.dll.dylib' (no such file, not in dyld cache), 'libUSER32.dll.dylib' (no such file)
	dlopen(/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/USER32.dll, 0x0001): tried: '/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/USER32.dll' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/USER32.dll' (no such file), '/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/USER32.dll' (no such file)
	dlopen(USER32.dll, 0x0001): tried: 'USER32.dll' (no such file), '/System/Volumes/Preboot/Cryptexes/OSUSER32.dll' (no such file), '/usr/lib/USER32.dll' (no such file, not in dyld cache), 'USER32.dll' (no such file)
	dlopen(/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/libUSER32.dll, 0x0001): tried: '/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/libUSER32.dll' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/libUSER32.dll' (no such file), '/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/libUSER32.dll' (no such file)
	dlopen(libUSER32.dll, 0x0001): tried: 'libUSER32.dll' (no such file), '/System/Volumes/Preboot/Cryptexes/OSlibUSER32.dll' (no such file), '/usr/lib/libUSER32.dll' (no such file, not in dyld cache), 'libUSER32.dll' (no such file)

	   at System.Runtime.InteropServices.NativeLibrary.LoadLibErrorTracker.Throw(String) + 0x46
	   at Internal.Runtime.CompilerHelpers.InteropHelpers.FixupModuleCell(InteropHelpers.ModuleFixupCell*) + 0x12c
	   at Internal.Runtime.CompilerHelpers.InteropHelpers.ResolvePInvokeSlow(InteropHelpers.MethodFixupCell*) + 0x35
	   at Windows.Win32.PInvoke.<SetProcessDpiAwarenessContext>g__LocalExternFunction|267_0(DPI_AWARENESS_CONTEXT) + 0x22
	   at Windows.Win32.PInvoke.SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT) + 0x1b
	   at DpiBootstrap.Init() + 0x10
	   at Internal.Runtime.CompilerHelpers.StartupCodeHelpers.RunInitializers(TypeManagerHandle, ReadyToRunSectionType) + 0x45
	   at Internal.Runtime.CompilerHelpers.StartupCodeHelpers.RunModuleInitializers() + 0x37
	…
	Exception: /Users/runner/work/1/s/build/test-scripts/run-netcore-mobile-template-tests.ps1:13
	Line |
	  13 |          throw "Exit code must be zero."
	     |          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	     | Exit code must be zero.

[0]: https://github.com/dotnet/runtime/blob/2e85cbda5839dc36705686fc4a3d6b50f2cef5a9/docs/design/specs/Ecma-335-Augments.md#module-initializer
[1]: https://github.com/dotnet/runtime/blob/b1cb4c502f1eec66227a31e705934fb67fc091f0/src/coreclr/nativeaot/Common/src/Internal/Runtime/CompilerHelpers/tartupCodeHelpers.cs#L163-L169%5D
[2]: https://github.com/unoplatform/uno.chefs/blob/af0a0c928337688c5ed3e87c3389a1cfdad46933/Chefs/Platforms/Desktop/Program.cs#L15
[3]: https://github.com/unoplatform/uno.licensing/tree/90428261b08e4115e9ce1f08d2799e2451340491/src/Uno.Settings
[4]: https://github.com/unoplatform/uno.licensing/blob/90428261b08e4115e9ce1f08d2799e2451340491/src/Uno.Settings/Uno.Settings/Platforms/Desktop/Program.cs#L17-L23

[^0]: An alternate approach was to remove the module initializer
      completely, and instead rely on `Win32Host` to init DPI.
      The problem with this approach is the [Uno.Settings app][3]:
      there is a requirement that it be launchable as
      `dotnet Uno.Settings.dll`, i.e. outside of an "app manifest"
      environment.  Additionally, DPI must be set *before* the Windows
      message loop is created.  Given that a message loop is possibly
      created [before the Uno Platform Host is created][4], the only
      reliable mechanism to ensure DPI is set properly that doesn't
      require a code change is via a module initializer.
jonpryor added a commit that referenced this pull request Nov 7, 2025
("Module initializers considered problematic?")

Context: #20443
Context: #20544
Context: #20557
Context: unoplatform/uno.chefs#1709

[Module Initializers][0] are code that:

> …is executed at, or sometime before, first access to any static field
> or first invocation of any method defined in the module.

In a CoreCLR or MonoVM context, a module initializer is typically
executed shortly after the assembly is loaded.

In a NativeAOT context, module initializers are executed
[*at startup*][1].  All of them.  All at once (sequentially).

This is fine…until you have a module initializer which assumes the
operating system that it's executing on.

Enter PR #20443, which introduced a module initializer which assumes
Windows.

This promptly broke Linux (#20544), the fix for which (#20557) was to
"only init DpiBootstrap on Windows".

Which works…so long as you have `DpiBootstrap` in a separate assembly
which is loaded on-demand.

Enter `src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent`,
which has a `.UseWin32()` invocation.  When run under NativeAOT:

	dotnet publish src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/*.csproj \
	  -c Release -r osx-x64 -p:UnoTargetFrameworkOverride=net10.0-desktop -f net10.0-desktop \
	  -p:SelfContained=true -p:PublishAot=true -bl

	src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/uno56netcurrent

the `.UseWin32()` invocation (eventually) causes
`Uno.UI.Runtime.Skia.Win32.Support.dll` to be included in the app and
the `DpiBootstrap` module initializer executed during startup, which
promptly fails in a manner similar to #20544:

	Unhandled exception. System.DllNotFoundException: Unable to load shared library 'USER32.dll' or one of its dependencies. In order to help diagnose loading problems, consider setting the DYLD_PRINT_LIBRARIES environment variable:
	…

A workaround for this is to use a `#if` to exclude the `.UseWin32()`
invocation when building for non-Windows platforms.  This allows the
trimmer to remove the reference to
`Uno.UI.Runtime.Skia.Win32.Support.dll`, avoiding its module
initializer; this is the approach taken in early drafts of
unoplatform/uno.chefs#1709.

Unfortunately, this doesn't "scale": we don't want every app to wrap
`.UseWin32()` invocations with a `#if`.

Update `DpiBootstrap` to wrap the `SetProcessDpiAwarenessContext()`
call in a `if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))`
block[^0].  This ensures the code is only run on Windows, and should
avoid the crash on non-Windows platforms.

Test this scenario by updating `run-netcore-mobile-template-tests.ps1`
to allow running a binary after the build and before the clean.

Update `src/SolutionTemplate/5.6/uno56netcurrent` to support a new
`--exit` command-line argument; when provided, `App.OnLaunched()` will
call `Application.Exit()`, causing the app to exit.  This allows us to
start the app and have it exit on CI.  If it crashes, then the exit
code will be a non-zero value, e.g.

	Executing: 5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/uno56netcurrent --exit

	Unhandled exception. System.DllNotFoundException: Unable to load shared library 'USER32.dll' or one of its dependencies. In order to help diagnose loading problems, consider setting the DYLD_PRINT_LIBRARIES environment variable:
	dlopen(/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/USER32.dll.dylib, 0x0001): tried: '/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/USER32.dll.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/USER32.dll.dylib' (no such file), '/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/USER32.dll.dylib' (no such file)
	dlopen(USER32.dll.dylib, 0x0001): tried: 'USER32.dll.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OSUSER32.dll.dylib' (no such file), '/usr/lib/USER32.dll.dylib' (no such file, not in dyld cache), 'USER32.dll.dylib' (no such file)
	dlopen(/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/libUSER32.dll.dylib, 0x0001): tried: '/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/libUSER32.dll.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/libUSER32.dll.dylib' (no such file), '/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/libUSER32.dll.dylib' (no such file)
	dlopen(libUSER32.dll.dylib, 0x0001): tried: 'libUSER32.dll.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OSlibUSER32.dll.dylib' (no such file), '/usr/lib/libUSER32.dll.dylib' (no such file, not in dyld cache), 'libUSER32.dll.dylib' (no such file)
	dlopen(/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/USER32.dll, 0x0001): tried: '/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/USER32.dll' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/USER32.dll' (no such file), '/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/USER32.dll' (no such file)
	dlopen(USER32.dll, 0x0001): tried: 'USER32.dll' (no such file), '/System/Volumes/Preboot/Cryptexes/OSUSER32.dll' (no such file), '/usr/lib/USER32.dll' (no such file, not in dyld cache), 'USER32.dll' (no such file)
	dlopen(/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/libUSER32.dll, 0x0001): tried: '/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/libUSER32.dll' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/libUSER32.dll' (no such file), '/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/libUSER32.dll' (no such file)
	dlopen(libUSER32.dll, 0x0001): tried: 'libUSER32.dll' (no such file), '/System/Volumes/Preboot/Cryptexes/OSlibUSER32.dll' (no such file), '/usr/lib/libUSER32.dll' (no such file, not in dyld cache), 'libUSER32.dll' (no such file)

	   at System.Runtime.InteropServices.NativeLibrary.LoadLibErrorTracker.Throw(String) + 0x46
	   at Internal.Runtime.CompilerHelpers.InteropHelpers.FixupModuleCell(InteropHelpers.ModuleFixupCell*) + 0x12c
	   at Internal.Runtime.CompilerHelpers.InteropHelpers.ResolvePInvokeSlow(InteropHelpers.MethodFixupCell*) + 0x35
	   at Windows.Win32.PInvoke.<SetProcessDpiAwarenessContext>g__LocalExternFunction|267_0(DPI_AWARENESS_CONTEXT) + 0x22
	   at Windows.Win32.PInvoke.SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT) + 0x1b
	   at DpiBootstrap.Init() + 0x10
	   at Internal.Runtime.CompilerHelpers.StartupCodeHelpers.RunInitializers(TypeManagerHandle, ReadyToRunSectionType) + 0x45
	   at Internal.Runtime.CompilerHelpers.StartupCodeHelpers.RunModuleInitializers() + 0x37
	…
	Exception: /Users/runner/work/1/s/build/test-scripts/run-netcore-mobile-template-tests.ps1:13
	Line |
	  13 |          throw "Exit code must be zero."
	     |          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	     | Exit code must be zero.

[0]: https://github.com/dotnet/runtime/blob/2e85cbda5839dc36705686fc4a3d6b50f2cef5a9/docs/design/specs/Ecma-335-Augments.md#module-initializer
[1]: https://github.com/dotnet/runtime/blob/b1cb4c502f1eec66227a31e705934fb67fc091f0/src/coreclr/nativeaot/Common/src/Internal/Runtime/CompilerHelpers/tartupCodeHelpers.cs#L163-L169%5D
[2]: https://github.com/unoplatform/uno.chefs/blob/af0a0c928337688c5ed3e87c3389a1cfdad46933/Chefs/Platforms/Desktop/Program.cs#L15
[3]: https://github.com/unoplatform/uno.licensing/tree/90428261b08e4115e9ce1f08d2799e2451340491/src/Uno.Settings
[4]: https://github.com/unoplatform/uno.licensing/blob/90428261b08e4115e9ce1f08d2799e2451340491/src/Uno.Settings/Uno.Settings/Platforms/Desktop/Program.cs#L17-L23

[^0]: An alternate approach was to remove the module initializer
      completely, and instead rely on `Win32Host` to init DPI.
      The problem with this approach is the [Uno.Settings app][3]:
      there is a requirement that it be launchable as
      `dotnet Uno.Settings.dll`, i.e. outside of an "app manifest"
      environment.  Additionally, DPI must be set *before* the Windows
      message loop is created.  Given that a message loop is possibly
      created [before the Uno Platform Host is created][4], the only
      reliable mechanism to ensure DPI is set properly that doesn't
      require a code change is via a module initializer.
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators May 7, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area/automation Categorizes an issue or PR as relevant to project automation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Crash on Linux due to DpiBootstrap static constructor

4 participants