Fix eager native SNI initialization on non-Windows platforms - #4700
Open
shreyarao4 wants to merge 1 commit into
Open
shreyarao4 wants to merge 1 commit into
shreyarao4 wants to merge 1 commit into
Conversation
Fixes a crash where SNILoadHandle's native SNI initialization (SniInitialize(), Windows-only) can run on non-Windows platforms, throwing DllNotFoundException
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
Contributor
|
That load behaviour is very eager - the code paths are unreachable as a result of |
Contributor
Author
|
I have not verified on any other platforms, but please note this is using Mono runtime. dotnet/runtime#77513 confirms this behavior on Mono. Mono eagerly runs a beforefieldinit type's static constructor even when the accessing code path is never taken. |
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.
Description
After #4465, the same SqlClient code is built for both Windows and non-Windows platforms, with native or managed SNI selected at runtime.
SNILoadHandle contains a static singleton:
internal static readonly SNILoadHandle SingletonInstance = new SNILoadHandle();Because SNILoadHandle has no explicit static constructor, its treated as beforefieldinit. This allows the runtime to initialize the type before SingletonInstance is actually accessed.
The SNILoadHandle constructor calls the Windows-only native SNI initialization:
SniNativeWrapper.SniInitialize();On non-Windows platforms, managed SNI is selected and SNILoadHandle should not be initialized. However, because of beforefieldinit, the runtime can initialize SNILoadHandle merely because it is referenced by JIT-compiled code.
This causes the Windows-only native SNI library to be loaded on non-Windows platforms and results in the following DllNotFoundException.
Solution
Added an explicit static constructor to SNILoadHandle.
This removes the beforefieldinit behavior and ensures that static initialization happens only when SNILoadHandle is actually accessed.
Therefore, on Linux, SNILoadHandle is not initialized when the managed SNI path is selected, and the Windows-only native SNI library is never loaded.
Testing
Verified on Linux/s390x: ManualTests previously crashed with the DllNotFoundException above; after this change, tests run sucessfully.
Environment
Linux s390x
Mono runtime
@giritrivedi