Skip to content

Move native-compat gate from [ModuleInitializer] to SkiaApi cctor - #4133

Merged
mattleibow merged 1 commit into
mainfrom
dev/issue-3817-no-module-init
Jun 11, 2026
Merged

mattleibow merged 1 commit into
mainfrom
dev/issue-3817-no-module-init

Conversation

@mattleibow

@mattleibow mattleibow commented Jun 10, 2026 •

Copy link
Copy Markdown
Contributor

Follow-up to #4080 (now merged). Based directly on main — a clean one-commit, 2-file delta.

Problem

After the singleton rework in #4080 removed the SKObject static constructor (which previously triggered the native-library version check), the check was re-added as a [ModuleInitializer].

A module initializer runs at assembly load — before any user code. That forces an eager P/Invoke into libSkiaSharp before a consumer can configure native-library loading (registering a DllImportResolver / NativeLibrary.SetDllImportResolver, or adjusting the search path). This breaks the "set up the native binary first, then use Skia later" pattern that works today.

Fix

Move the compatibility gate into SkiaApi's static constructor and delete SkiaSharpModuleInitializer.cs.

SkiaApi is the single internal type through which every P/Invoke into libSkiaSharp flows. Defining an explicit static constructor strips its beforefieldinit flag, so the CLR guarantees the check runs before the first sk_* call — on every interop configuration (USE_DELEGATES, USE_LIBRARY_IMPORT, direct DllImport) and every platform — without running at assembly load. This recovers the friendly "incompatible version range" message for all consumers (including code that only touches SKBitmap, SKCanvas, SKMatrix, or SKGraphics) while restoring deferred, post-configuration native loading.

Why it's safe

  • Re-entrancy: CheckNativeLibraryCompatible reaches native via SkiaApi.sk_version_get_milestone/sk_version_get_increment while the cctor is still running. The CLR's same-thread type-initializer rule returns the in-progress SkiaApi type without re-running the body, and the only static state the version path reads (the USE_DELEGATES Lazy<IntPtr> handle + delegate cache) is a field initializer, which runs before the cctor body.
  • No [BUG] SKFontManager.Default throws an exception #3817 regression: the cctor body touches only SkiaApi/LibraryLoader, never an SKObject, so it cannot reintroduce the static-initializer cycle.
  • Negligible cost: losing beforefieldinit adds a type-init readiness check that is masked by the P/Invoke transition itself (~0.35% on CoreCLR, statistically zero on Mono/net48).

Validation

  • Builds clean on net48, net9.0, netstandard2.0.
  • Version + singleton-init + colorspace tests pass locally (35/35, net10).

Co-authored-by: Copilot [email protected]

@github-actions

github-actions Bot commented Jun 10, 2026 •

Copy link
Copy Markdown
Contributor

📦 Try the packages from this PR

Warning

Do not run these scripts without first reviewing the code in this PR.

Step 1 — Download the packages

bash / macOS / Linux:

curl -fsSL https://raw.githubusercontent.com/mono/SkiaSharp/main/scripts/get-skiasharp-pr.sh | bash -s -- 4133

PowerShell / Windows:

iex "& { $(irm https://raw.githubusercontent.com/mono/SkiaSharp/main/scripts/get-skiasharp-pr.ps1) } 4133"

Step 2 — Add the local NuGet source

dotnet nuget add source ~/.skiasharp/hives/pr-4133/packages --name skiasharp-pr-4133
More options
Option Description
--successful-only / -SuccessfulOnly Only use successful builds
--force / -Force Overwrite previously downloaded packages
--list / -List List available artifacts without downloading
--build-id ID / -BuildId ID Download from a specific build

Or download manually from Azure Pipelines — look for the nuget artifact on the build for this PR.

Remove the source when you're done:

dotnet nuget remove source skiasharp-pr-4133

@mattleibow
mattleibow changed the base branch from main to dev/issue-3817-fix-singleton-init June 10, 2026 18:36
@mattleibow
mattleibow changed the base branch from dev/issue-3817-fix-singleton-init to dev/issue-3817-4080-base June 10, 2026 18:38
@github-actions

github-actions Bot commented Jun 10, 2026 •

Copy link
Copy Markdown
Contributor

📖 Documentation Preview

The documentation for this PR has been deployed and is available at:

🔗 View Staging Site
🔗 View Staging Docs
🔗 View Staging Gallery (Blazor)
🔗 View Staging Gallery (Uno Platform)
🔗 View Staging SkiaFiddle

This preview will be updated automatically when you push new commits to this PR.


This comment is automatically updated by the documentation staging workflow.

@mattleibow
mattleibow changed the base branch from dev/issue-3817-4080-base to main June 10, 2026 22:14
@mattleibow
mattleibow force-pushed the dev/issue-3817-no-module-init branch from a228bcd to 00c13ef Compare June 10, 2026 22:20
PR #4080 added a [ModuleInitializer] to run SkiaSharpVersion.CheckNative-
LibraryCompatible after the singleton rework removed the SKObject static
constructor that previously triggered it. A module initializer runs at
assembly load, which forces an eager P/Invoke into libSkiaSharp before a
consumer can configure native-library loading (a DllImportResolver /
NativeLibrary.SetDllImportResolver, or a custom search path). That breaks
the 'configure the native binary first, then use Skia later' pattern.

Move the gate into SkiaApi's static constructor instead. SkiaApi is the
single internal type every P/Invoke flows through, so the CLR guarantees
the check runs before the first sk_* call on every interop configuration
(USE_DELEGATES, USE_LIBRARY_IMPORT, direct DllImport) without running at
assembly load. The re-entrant version probe is safe: the same-thread CLR
type-initializer rule returns the in-progress SkiaApi type, and the only
static state the version path reads is field-initialized before the cctor
body. The body touches no SKObject, so it cannot reintroduce the #3817
static-initializer cycle.

Deletes SkiaSharpModuleInitializer.cs.

Co-authored-by: Copilot <[email protected]>
@mattleibow
mattleibow force-pushed the dev/issue-3817-no-module-init branch from 00c13ef to 89f1e36 Compare June 11, 2026 10:59
@mattleibow
mattleibow merged commit b571381 into main Jun 11, 2026
3 of 4 checks passed
@mattleibow
mattleibow deleted the dev/issue-3817-no-module-init branch June 11, 2026 11:00
@mattleibow mattleibow added this to the 4.148.0-rc.1 milestone Jun 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant