Skip to content

fix: extend Android Surface lifetime to avoid GC-related crash - #23927

Merged
jonpryor merged 1 commit into
masterfrom
dev/jonpryor/jonp-prevent-ANativeWindow_fromSurface-crash
Jul 30, 2026
Merged

jonpryor merged 1 commit into
masterfrom
dev/jonpryor/jonp-prevent-ANativeWindow_fromSurface-crash

Conversation

@jonpryor

@jonpryor jonpryor commented Jul 30, 2026 •

Copy link
Copy Markdown
Contributor

Fixes: #23923

Context: https://learn.microsoft.com/en-us/archive/blogs/cbrumme/lifetime-gc-keepalive-handle-recycling
Context: mono/SkiaSharp#3393

Through (good? bad? dumb?) luck, when running SamplesApp on my handy emulator, building SamplesApp from a commit on the feature/breakingchanges branch:

# note: commit 0ebec is on the feature/breakingchanges branch
git checkout 0ebec1e321fc5ff5c21ed166130f69241ddcfbb8
# note: feature/breakingchanges is on .NET 11 P5
export PATH=path/to/dotnet-sdk-11.0.100-preview.5.26302.115:$PATH
\cp build/ci/net11/_global.json global.json
dotnet publish src/SamplesApp/SamplesApp.Skia.netcoremobile/SamplesApp.Skia.netcoremobile.csproj -r android-arm64 -f net11.0-android -p:UnoTargetFrameworkOverride=net11.0-android
adb install src/SamplesApp/SamplesApp.Skia.netcoremobile/bin/Release/net11.0-android/android-arm64/publish/uno.platform.samplesapp.skia-Signed.apk
adb shell am start uno.platform.samplesapp.skia/crc6448f3b0362cbf4bc9.MainActivity

Immediatley after launching the app, it would crash, with adb logcat containing:

ActivityManager: Displayed uno.platform.samplesapp.skia/crc6448f3b0362cbf4bc9.MainActivity: +1s380ms
samplesapp.ski: indirect_reference_table.cc:59] JNI ERROR (app bug): attempt to use stale Global 0x3916 (should be 0x391a)
samplesapp.ski: runtime.cc:558] Runtime aborting...
samplesapp.ski: runtime.cc:558] Dumping all threads without appropriate locks held: thread list lock
samplesapp.ski: runtime.cc:558] All threads:
samplesapp.ski: runtime.cc:558] DALVIK THREADS (20):
samplesapp.ski: runtime.cc:558] "Thread-8" prio=10 tid=19 Runnable
samplesapp.ski: runtime.cc:558]   | group="" sCount=0 dsCount=0 flags=0 obj=0x12d01368 self=0x6f186fa800
samplesapp.ski: runtime.cc:558]   | sysTid=7366 nice=-10 cgrp=default sched=0/0 handle=0x6f1993f4f0
samplesapp.ski: runtime.cc:558]   | state=R schedstat=( 3289249 4228251 22 ) utm=0 stm=0 core=2 HZ=100
samplesapp.ski: runtime.cc:558]   | stack=0x6f19844000-0x6f19846000 stackSize=1009KB
samplesapp.ski: runtime.cc:558]   | held mutexes= "abort lock" "mutator lock"(shared held)
samplesapp.ski: runtime.cc:558]   native: #00 pc 00000000003b49a0  /system/lib64/libart.so (art::DumpNativeStack(std::__1::basic_ostream<char, std::__1::char_traits<char>>&, int, BacktraceMap*, char const*, art::ArtMethod*, void*, bool)+216)
samplesapp.ski: runtime.cc:558]   native: #01 pc 0000000000480e20  /system/lib64/libart.so (art::Thread::DumpStack(std::__1::basic_ostream<char, std::__1::char_traits<char>>&, bool, BacktraceMap*, bool) const+348)
samplesapp.ski: runtime.cc:558]   native: #02 pc 000000000049a598  /system/lib64/libart.so (art::DumpCheckpoint::Run(art::Thread*)+812)
samplesapp.ski: runtime.cc:558]   native: #03 pc 000000000049350c  /system/lib64/libart.so (art::ThreadList::RunCheckpoint(art::Closure*, art::Closure*)+464)
samplesapp.ski: runtime.cc:558]   native: #04 pc 00000000004927d4  /system/lib64/libart.so (art::ThreadList::Dump(std::__1::basic_ostream<char, std::__1::char_traits<char>>&, bool)+504)
samplesapp.ski: runtime.cc:558]   native: #05 pc 00000000004564dc  /system/lib64/libart.so (art::Runtime::Abort(char const*)+380)
samplesapp.ski: runtime.cc:558]   native: #06 pc 0000000000008cb0  /system/lib64/libbase.so (android::base::LogMessage::~LogMessage()+724)
samplesapp.ski: runtime.cc:558]   native: #07 pc 00000000002356e0  /system/lib64/libart.so (art::IndirectReferenceTable::AbortIfNoCheckJNI(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const&)+236)
samplesapp.ski: runtime.cc:558]   native: #08 pc 00000000002e2790  /system/lib64/libart.so (art::IndirectReferenceTable::GetChecked(void*) const+436)
samplesapp.ski: runtime.cc:558]   native: #09 pc 00000000002ddabc  /system/lib64/libart.so (art::JavaVMExt::DecodeGlobal(void*)+24)
samplesapp.ski: runtime.cc:558]   native: #10 pc 0000000000486734  /system/lib64/libart.so (art::Thread::DecodeJObject(_jobject*) const+152)
samplesapp.ski: runtime.cc:558]   native: #11 pc 00000000003439c4  /system/lib64/libart.so (art::JNI::GetObjectField(_JNIEnv*, _jobject*, _jfieldID*)+596)
samplesapp.ski: runtime.cc:558]   native: #12 pc 00000000000ff6bc  /system/lib64/libandroid_runtime.so (android::android_view_Surface_getSurface(_JNIEnv*, _jobject*)+52)
samplesapp.ski: runtime.cc:558]   native: #13 pc 00000000000ff64c  /system/lib64/libandroid_runtime.so (android::android_view_Surface_getNativeWindow(_JNIEnv*, _jobject*)+36)
samplesapp.ski: runtime.cc:558]   native: #14 pc 0000000000012394  /system/lib64/libandroid.so (ANativeWindow_fromSurface+32)
samplesapp.ski: runtime.cc:558]   native: #15 pc 00000000064927d0  <anonymous:6fd9e70000> (???)
samplesapp.ski: runtime.cc:558]   (no managed stack frames)

For expository purposes, the relevant code is:

/* 1 */ var surface = holder.Surface;
/* 2 */ IntPtr __env = JNIEnv.Handle;
/* 3 */ IntPtr __surface = surface.Handle;
/* 4 */ _nativeWindow = ANativeWindow_fromSurface(__env, __surface);

What appears to be happening is the following:

  1. .NET 11 P5 changes the default Android runtime to CoreCLR. (.NET 11 P6 makes using MonoVM an error…)

    This means that we're now using a different GC than .NET 10+MonoVM.

  2. The surface instance created on line 1 is not referenced again after line 3, which means it is eligible for collection immediately after the surface.Handle property invocation.

  3. If the GC collects the surface instance between lines 3 and 4, then __surface will be an invalid JNI handle, as the GC collecting surface will invalidate it.

  4. If __surface is an invalid handle, then ANativeWindow_fromSurface() will (rightfully!) crash with the "use [of] stale Global [JNI object reference].""

The fix for this, in a manner similar to that mentioned in mono/SkiaSharp#3393, is to extend the lifetime of surface so that the GC doesn't attempt to collect it before/during the ANativeWindow_fromSurface() invocation, by using GC.KeepAlive():

var surface = holder.Surface;
IntPtr __env = JNIEnv.Handle;
IntPtr __surface = surface.Handle;
_nativeWindow = ANativeWindow_fromSurface(__env, __surface);
GC.KeepAlive(surface);

With this understanding in mind, review other use of JNI-backed .Handle use within src/Uno.UI.Runtime.Skia.Android:

git grep '\.Handle\>' src/Uno.UI.Runtime.Skia.Android

and add GC.KeepAlive() invocations to ensure that the instance upon which .Handle is invoked is kept alive around the method call using .Handle.

GitHub Issue: closes #23923

PR Type:

🐞 Bugfix

What changed? 🚀

PR Checklist ✅

Fixes: #23923

Context: https://learn.microsoft.com/en-us/archive/blogs/cbrumme/lifetime-gc-keepalive-handle-recycling
Context: mono/SkiaSharp#3393

Through (good? bad? dumb?) luck, when running SamplesApp on my handy
emulator, building SamplesApp from a commit on the
feature/breakingchanges branch:

	# note: commit 0ebec is on the feature/breakingchanges branch
	git checkout 0ebec1e
	# note: feature/breakingchanges is on .NET 11 P5
	export PATH=path/to/dotnet-sdk-11.0.100-preview.5.26302.115:$PATH
	\cp build/ci/net11/_global.json global.json
	dotnet publish src/SamplesApp/SamplesApp.Skia.netcoremobile/SamplesApp.Skia.netcoremobile.csproj -r android-arm64 -f net11.0-android -p:UnoTargetFrameworkOverride=net11.0-android
	adb install src/SamplesApp/SamplesApp.Skia.netcoremobile/bin/Release/net11.0-android/android-arm64/publish/uno.platform.samplesapp.skia-Signed.apk
	adb shell am start uno.platform.samplesapp.skia/crc6448f3b0362cbf4bc9.MainActivity

Immediatley after launching the app, it would crash, with `adb logcat`
containing:

	ActivityManager: Displayed uno.platform.samplesapp.skia/crc6448f3b0362cbf4bc9.MainActivity: +1s380ms
	samplesapp.ski: indirect_reference_table.cc:59] JNI ERROR (app bug): attempt to use stale Global 0x3916 (should be 0x391a)
	samplesapp.ski: runtime.cc:558] Runtime aborting...
	samplesapp.ski: runtime.cc:558] Dumping all threads without appropriate locks held: thread list lock
	samplesapp.ski: runtime.cc:558] All threads:
	samplesapp.ski: runtime.cc:558] DALVIK THREADS (20):
	samplesapp.ski: runtime.cc:558] "Thread-8" prio=10 tid=19 Runnable
	samplesapp.ski: runtime.cc:558]   | group="" sCount=0 dsCount=0 flags=0 obj=0x12d01368 self=0x6f186fa800
	samplesapp.ski: runtime.cc:558]   | sysTid=7366 nice=-10 cgrp=default sched=0/0 handle=0x6f1993f4f0
	samplesapp.ski: runtime.cc:558]   | state=R schedstat=( 3289249 4228251 22 ) utm=0 stm=0 core=2 HZ=100
	samplesapp.ski: runtime.cc:558]   | stack=0x6f19844000-0x6f19846000 stackSize=1009KB
	samplesapp.ski: runtime.cc:558]   | held mutexes= "abort lock" "mutator lock"(shared held)
	samplesapp.ski: runtime.cc:558]   native: #00 pc 00000000003b49a0  /system/lib64/libart.so (art::DumpNativeStack(std::__1::basic_ostream<char, std::__1::char_traits<char>>&, int, BacktraceMap*, char const*, art::ArtMethod*, void*, bool)+216)
	samplesapp.ski: runtime.cc:558]   native: #1 pc 0000000000480e20  /system/lib64/libart.so (art::Thread::DumpStack(std::__1::basic_ostream<char, std::__1::char_traits<char>>&, bool, BacktraceMap*, bool) const+348)
	samplesapp.ski: runtime.cc:558]   native: #2 pc 000000000049a598  /system/lib64/libart.so (art::DumpCheckpoint::Run(art::Thread*)+812)
	samplesapp.ski: runtime.cc:558]   native: #3 pc 000000000049350c  /system/lib64/libart.so (art::ThreadList::RunCheckpoint(art::Closure*, art::Closure*)+464)
	samplesapp.ski: runtime.cc:558]   native: #4 pc 00000000004927d4  /system/lib64/libart.so (art::ThreadList::Dump(std::__1::basic_ostream<char, std::__1::char_traits<char>>&, bool)+504)
	samplesapp.ski: runtime.cc:558]   native: #5 pc 00000000004564dc  /system/lib64/libart.so (art::Runtime::Abort(char const*)+380)
	samplesapp.ski: runtime.cc:558]   native: #6 pc 0000000000008cb0  /system/lib64/libbase.so (android::base::LogMessage::~LogMessage()+724)
	samplesapp.ski: runtime.cc:558]   native: #7 pc 00000000002356e0  /system/lib64/libart.so (art::IndirectReferenceTable::AbortIfNoCheckJNI(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const&)+236)
	samplesapp.ski: runtime.cc:558]   native: #8 pc 00000000002e2790  /system/lib64/libart.so (art::IndirectReferenceTable::GetChecked(void*) const+436)
	samplesapp.ski: runtime.cc:558]   native: #9 pc 00000000002ddabc  /system/lib64/libart.so (art::JavaVMExt::DecodeGlobal(void*)+24)
	samplesapp.ski: runtime.cc:558]   native: #10 pc 0000000000486734  /system/lib64/libart.so (art::Thread::DecodeJObject(_jobject*) const+152)
	samplesapp.ski: runtime.cc:558]   native: #11 pc 00000000003439c4  /system/lib64/libart.so (art::JNI::GetObjectField(_JNIEnv*, _jobject*, _jfieldID*)+596)
	samplesapp.ski: runtime.cc:558]   native: #12 pc 00000000000ff6bc  /system/lib64/libandroid_runtime.so (android::android_view_Surface_getSurface(_JNIEnv*, _jobject*)+52)
	samplesapp.ski: runtime.cc:558]   native: #13 pc 00000000000ff64c  /system/lib64/libandroid_runtime.so (android::android_view_Surface_getNativeWindow(_JNIEnv*, _jobject*)+36)
	samplesapp.ski: runtime.cc:558]   native: #14 pc 0000000000012394  /system/lib64/libandroid.so (ANativeWindow_fromSurface+32)
	samplesapp.ski: runtime.cc:558]   native: #15 pc 00000000064927d0  <anonymous:6fd9e70000> (???)
	samplesapp.ski: runtime.cc:558]   (no managed stack frames)

For expository purposes, the relevant code is:

	/* 1 */ var surface = holder.Surface;
	/* 2 */ IntPtr __env = JNIEnv.Handle;
	/* 3 */ IntPtr __surface = surface.Handle;
	/* 4 */ _nativeWindow = ANativeWindow_fromSurface(__env, __surface);

What appears to be happening is the following:

 1. .NET 11 P5 changes the default Android runtime to CoreCLR.
    (.NET 11 P6 makes using MonoVM an *error*…)

    This means that we're now using a different GC than .NET 10+MonoVM.

 2. The `surface` instance created on line 1 is not referenced again
    after line 3, which means it is eligible for collection immediately
    after the `surface.Handle` property invocation.

 4. If the GC collects the `surface` instance between lines 3 and 4,
    then `__surface` will be an *invalid JNI handle*, as the GC
    collecting `surface` will invalidate it.

 5. If `__surface` is an invalid handle, then
    `ANativeWindow_fromSurface()` will (rightfully!) crash with the
    "use [of] stale Global [JNI object reference].""

The fix for this, in a manner similar to that mentioned in
mono/SkiaSharp#3393, is to extend the lifetime of `surface` so that
the GC doesn't attempt to collect it before/during the
`ANativeWindow_fromSurface()` invocation, by using `GC.KeepAlive()`:

	var surface = holder.Surface;
	IntPtr __env = JNIEnv.Handle;
	IntPtr __surface = surface.Handle;
	_nativeWindow = ANativeWindow_fromSurface(__env, __surface);
	GC.KeepAlive(surface);

With this understanding in mind, review other use of `.Handle`
within src/Uno.UI.Runtime.Skia.Android:

	git grep '\.Handle\>' src/Uno.UI.Runtime.Skia.Android

and add `GC.KeepAlive()` invocations to ensure that the instance upon
which `.Handle` is invoked is kept alive around the method call using
`.Handle`.
Copilot AI review requested due to automatic review settings July 30, 2026 19:01
@github-actions github-actions Bot added platform/android 🤖 Categorizes an issue or PR as relevant to the Android platform area/skia ✏️ Categorizes an issue or PR as relevant to Skia labels Jul 30, 2026
@jonpryor
jonpryor requested a review from ramezgerges July 30, 2026 19:01

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.

Pull request overview

This PR addresses an Android crash scenario by extending managed object lifetimes around JNI/native calls so CoreCLR GC cannot invalidate JNI handles prematurely, reducing the likelihood of GC-related “stale Global” failures during Vulkan surface initialization and related JNI invocations.

Changes:

  • Add GC.KeepAlive(surface) after ANativeWindow_fromSurface(...) to keep the Surface wrapper alive across the native call.
  • Add GC.KeepAlive(node) after JNIEnv.CallVoidMethod(...) to keep AccessibilityNodeInfoCompat alive across the JNI call.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/Uno.UI.Runtime.Skia.Android/Rendering/UnoSKVulkanView.cs Keeps Surface alive across ANativeWindow_fromSurface to avoid GC invalidating the JNI handle.
src/Uno.UI.Runtime.Skia.Android/Accessibility/AccessibilityNodeInfoCompatJni.cs Keeps AccessibilityNodeInfoCompat alive across JNIEnv.CallVoidMethod to avoid GC invalidating the JNI handle.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@unodevops

Copy link
Copy Markdown
Contributor

@jonpryor
jonpryor enabled auto-merge July 30, 2026 20:55
@unodevops

Copy link
Copy Markdown
Contributor

⚠️⚠️ The build 225075 has failed on Uno.UI - CI.

@jonpryor
jonpryor merged commit 8e5da92 into master Jul 30, 2026
52 checks passed
@jonpryor
jonpryor deleted the dev/jonpryor/jonp-prevent-ANativeWindow_fromSurface-crash branch July 30, 2026 21:18
MartinZikmund added a commit to ramezgerges/uno that referenced this pull request Sep 21, 2026
Restores unoplatform#23927: surface can be collected between the interop
call resolving its handle and ANativeWindow_fromSurface actually
using it, since nothing after the call keeps it alive. GC.KeepAlive
pins it through the call, as master already does for the
same interop pattern elsewhere.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01XsQppKuZgijApgJxaTZt81
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/skia ✏️ Categorizes an issue or PR as relevant to Skia platform/android 🤖 Categorizes an issue or PR as relevant to the Android platform

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Uno 7: Crash on Android with Vulkan

5 participants