Repository navigation
Move native-compat gate from [ModuleInitializer] to SkiaApi cctor - #4133
Merged
Merged
Conversation
Contributor
📦 Try the packages from this PRWarning 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 -- 4133PowerShell / 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-4133More options
Or download manually from Azure Pipelines — look for the Remove the source when you're done: dotnet nuget remove source skiasharp-pr-4133 |
mattleibow
changed the base branch from
main
to
dev/issue-3817-fix-singleton-init
June 10, 2026 18:36
mattleibow
changed the base branch from
dev/issue-3817-fix-singleton-init
to
dev/issue-3817-4080-base
June 10, 2026 18:38
Contributor
|
📖 Documentation Preview The documentation for this PR has been deployed and is available at: 🔗 View Staging Site 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
force-pushed
the
dev/issue-3817-no-module-init
branch
from
June 10, 2026 22:20
a228bcd to
00c13ef
Compare
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
force-pushed
the
dev/issue-3817-no-module-init
branch
from
June 11, 2026 10:59
00c13ef to
89f1e36
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
SKObjectstatic 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
libSkiaSharpbefore a consumer can configure native-library loading (registering aDllImportResolver/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 deleteSkiaSharpModuleInitializer.cs.SkiaApiis the single internal type through which every P/Invoke intolibSkiaSharpflows. Defining an explicit static constructor strips itsbeforefieldinitflag, so the CLR guarantees the check runs before the firstsk_*call — on every interop configuration (USE_DELEGATES,USE_LIBRARY_IMPORT, directDllImport) and every platform — without running at assembly load. This recovers the friendly "incompatible version range" message for all consumers (including code that only touchesSKBitmap,SKCanvas,SKMatrix, orSKGraphics) while restoring deferred, post-configuration native loading.Why it's safe
CheckNativeLibraryCompatiblereaches native viaSkiaApi.sk_version_get_milestone/sk_version_get_incrementwhile the cctor is still running. The CLR's same-thread type-initializer rule returns the in-progressSkiaApitype without re-running the body, and the only static state the version path reads (theUSE_DELEGATESLazy<IntPtr>handle + delegate cache) is a field initializer, which runs before the cctor body.SkiaApi/LibraryLoader, never anSKObject, so it cannot reintroduce the static-initializer cycle.beforefieldinitadds a type-init readiness check that is masked by the P/Invoke transition itself (~0.35% on CoreCLR, statistically zero on Mono/net48).Validation
net48,net9.0,netstandard2.0.Co-authored-by: Copilot [email protected]