Skip to content

[CoreCLR] Avoid main-thread dispatch for unknown P/Invokes - #12863

Merged
simonrozsival merged 2 commits into
mainfrom
simonrozsival-fix-coreclr-pinvoke-deadlocks
Sep 22, 2026
Merged

simonrozsival merged 2 commits into
mainfrom
simonrozsival-fix-coreclr-pinvoke-deadlocks

Conversation

@simonrozsival

Copy link
Copy Markdown
Member

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. GLSurfaceView can synchronously wait for its GL thread while the GL thread resolves its first libGLESv2 P/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 exported JNI_OnLoad, stores the result in DSOCacheEntry.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:

  • call DsoLoader::load with is_jni = false;
  • skip the filesystem existence check so linker names such as libGLESv2.so are passed directly to dlopen();
  • leave cache-known libraries unchanged so their generated JNI metadata continues to select 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 first libGLESv2 P/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):

  • Before the fix: the deterministic regression fails with The worker P/Invoke waited for the blocked Android main thread.
  • After the fix: the same regression passes.

The issue's supplied MinimalGLRepro is timing-sensitive and did not enter its full deadlock on this Android 16 device in repeated launches. It did confirm the loader-path change:

  • Before: each first GL entry point logged Trying to load shared JNI library libGLESv2.so and Running DSO loader ... dispatching to main thread; the first ClearColor/Viewport calls took approximately 156 ms / 28 ms on the initial launch.
  • After: the calls logged direct filesystem/native loading through dlopen() with no main-thread dispatch; the first ClearColor/Viewport calls took approximately 0.6 ms / 0.5 ms.

Validation

  • Built src/native/native-clr.csproj in Debug.
  • Built tests/MSBuildDeviceIntegration/MSBuildDeviceIntegration.csproj in Debug.
  • Ran UnknownSystemLibraryLoadsWithoutMainThreadDispatch on the Samsung Galaxy A16.
  • Ran the issue-provided MinimalGLRepro before and after the fix on the same device.
  • Reviewed the final diff with GPT-6 Astra; no significant issues were found.

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>
Copilot AI lite review requested due to automatic review settings September 22, 2026 10:17

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
@simonrozsival simonrozsival added this to the .NET 11 milestone Sep 22, 2026
@simonrozsival
simonrozsival merged commit 863bacf into main Sep 22, 2026
44 checks passed
@simonrozsival
simonrozsival deleted the simonrozsival-fix-coreclr-pinvoke-deadlocks branch September 22, 2026 14:29
@kagaminehaku

Copy link
Copy Markdown

Thanks for the fix! Would it be possible to backport this to .NET 10 (release/10.0.1xx)?

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 ChoreographerTimer makes its first P/Invoke into libandroid.so (AChoreographer_getInstance) on its own "Choreographer Thread". Meanwhile the main thread waits for it in AndroidPlatform.Initialize, because the compositor's first frame needs the choreographer. The loader dispatches System.loadLibrary to the blocked main thread, and each of the two name attempts times out:

W/monodroid-assembly: Timeout while waiting for shared library 'libandroid.so' to load.
W/monodroid-assembly: Timeout while waiting for shared library 'android' to load.
W/monodroid-assembly: Shared library 'android' not loaded, p/invoke 'AChoreographer_getInstance' may fail

Measurements (cold start to first frame, Release, ReadyToRun composite):

Device Without workaround With workaround
Red Magic 9 Pro+ (Android 15) ~6.5 s ~0.3 s
Galaxy S7 (LineageOS, Android 14) ~7.6 s ~1.4 s

The 6 s was the same in every run, with OpenGL ES or Vulkan alike. Mono does not have this problem.

Workaround: call AChoreographer_getInstance() once on the main thread before Avalonia starts, so the library is already loaded when the timer thread needs it.

Backport looks simple: release/10.0.1xx still has the same return DsoLoader::load (name, flags, true /* is_jni */); cache-miss branch in monodroid-dl.hh, and DsoLoader::load<SkipExistsCheck> already exists there, so the 6-line change should apply as is.

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 release/10.0.1xx would help, I'm happy to open one.

Environment: .NET SDK 10.0.401, Android workload 36.1.69, Avalonia 12.1.3, arm64.

kagaminehaku added a commit to kagaminehaku/OpenShiina that referenced this pull request Oct 12, 2026
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.
@jonathanpeppers

Copy link
Copy Markdown
Member

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/

@github-actions github-actions Bot locked and limited conversation to collaborators Nov 12, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[CoreCLR] [P/Invoke] Possible deadlocks on system library calls

4 participants