Repository navigation
[CoreCLR] Avoid main-thread dispatch for unknown P/Invokes - #12863
Conversation
Treat libraries absent from the generated DSO cache as non-JNI and load them directly. Add a device regression which blocks the main thread while a worker resolves libGLESv2. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Copilot review overview
🔵 Needs a closer look
Unresolved loading behavior and regression-test reliability issues remain.
Review effort: Lite
Findings: None
What changed in this PR
Updates CoreCLR to avoid main-thread dispatch for unknown system-library P/Invokes.
Changes:
- Loads DSO-cache misses directly via
dlopen(). - Adds device regression coverage for
libGLESv2.
| File | Summary |
|---|---|
src/native/clr/include/runtime-base/monodroid-dl.hh |
Changes cache-miss loading behavior. Moderate issue: cache misses may include runtime-added JNI libraries. |
tests/MSBuildDeviceIntegration/Tests/NativeLibraryLoadTests.cs |
Adds regression coverage. Moderate issues: task failures are not observed, and timeout thresholds are not centralized or documented. |
Delay the worker P/Invoke so the Android main thread has entered its blocking wait before native library resolution begins. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
|
Thanks for the fix! Would it be possible to backport this to .NET 10 ( We hit the same deadlock in an Avalonia app on .NET 10 + CoreCLR, without any GL code of our own: every cold start was 6 s slower than on Mono. What happens: Avalonia's Measurements (cold start to first frame, Release, ReadyToRun composite):
The 6 s was the same in every run, with OpenGL ES or Vulkan alike. Mono does not have this problem. Workaround: call Backport looks simple: This probably affects every Avalonia app on CoreCLR with .NET 10. #10914 looks like the same symptom (6 s on CoreCLR vs 1 s on Mono). I understand CoreCLR is still experimental in .NET 10, but since the fix is small and low-risk, a servicing backport would help everyone trying CoreCLR on the LTS release. If a community PR against Environment: .NET SDK 10.0.401, Android workload 36.1.69, Avalonia 12.1.3, arm64. |
On .NET 10, CoreCLR's Android host loads a library the app does not ship by sending System.loadLibrary to the main thread when the first P/Invoke into it comes from another thread, and gives up after 3 s for each of its two names. Avalonia's frame timer makes its first call into libandroid.so on its own thread while the main thread waits for it, so every start took 6 s more (logcat: "Timeout while waiting for shared library 'libandroid.so' to load"; dotnet/android#12826, fixed for .NET 11 by dotnet/android#12863). MainApplication calls AChoreographer_getInstance once on the main thread first: the first frame of the library at 0.27 s on a Red Magic 9 Pro+ (6.5 s before, Mono 0.46 s), 1.4 s on a Galaxy S7 (7.6 s). startup.log in the player folder gives each step of the start from the process's start: the runtime's own, Avalonia's steps and platform services, its log, the library read, the first frame. The library makes its settings when they are opened (they could start the GPU) and reads the games' .exe icons on a pool thread.
|
CoreCLR is experimental in .NET 10, so we normally don't backport fixes for it. .NET 11 is "go live" can you use it instead? https://devblogs.microsoft.com/dotnet/dotnet-11-rc-1/ |
Fixes #12826
Motivation
CoreCLR currently treats every native library absent from the generated DSO cache as a potential JNI library. On a managed worker or GL thread, this routes
System.loadLibrary()synchronously through the Android main looper.That is unsafe for Android system-library P/Invokes.
GLSurfaceViewcan synchronously wait for its GL thread while the GL thread resolves its firstlibGLESv2P/Invoke. The GL thread then waits for the main thread to process the library-load callback, producing a circular wait until the loader timeout.The generated DSO cache already contains build-time JNI metadata for every native library packaged into the application.
ELFHelper.IsJniLibrary()detects an exportedJNI_OnLoad, stores the result inDSOCacheEntry.is_jni_library, and JNI libraries are preloaded while the correct Java class-loader context is available. Reaching the cache-miss branch therefore means there is no packaged-library metadata to apply.Approach
Treat a DSO-cache miss as a platform or other non-JNI library:
DsoLoader::loadwithis_jni = false;libGLESv2.soare passed directly todlopen();System.loadLibrary()where required.This avoids maintaining a separate list of Android system libraries and makes the existing build-time metadata authoritative for packaged libraries.
Regression coverage
Adds
UnknownSystemLibraryLoadsWithoutMainThreadDispatch, which deliberately blocks the Android main thread while a worker performs its firstlibGLESv2P/Invoke. The worker must finish within two seconds, before the existing three-second main-thread loader timeout.On a Samsung Galaxy A16 (
SM-A165F, Android 16, arm64-v8a):The worker P/Invoke waited for the blocked Android main thread.The issue's supplied
MinimalGLReprois timing-sensitive and did not enter its full deadlock on this Android 16 device in repeated launches. It did confirm the loader-path change:Trying to load shared JNI library libGLESv2.soandRunning DSO loader ... dispatching to main thread; the firstClearColor/Viewportcalls took approximately 156 ms / 28 ms on the initial launch.dlopen()with no main-thread dispatch; the firstClearColor/Viewportcalls took approximately 0.6 ms / 0.5 ms.Validation
src/native/native-clr.csprojin Debug.tests/MSBuildDeviceIntegration/MSBuildDeviceIntegration.csprojin Debug.UnknownSystemLibraryLoadsWithoutMainThreadDispatchon the Samsung Galaxy A16.MinimalGLReprobefore and after the fix on the same device.