Repository navigation
Description
Current behavior 🐛
May be a heisenbug; recording here for posterity/investigation.
I am observing a crash from ANativeWindow_fromSurface() when running SamplesApp on an Android emulator.
Expected behavior 🎯
No crash.
How to reproduce it (as minimally and precisely as possible) 🔬
SamplesApp is in the unoplatform/uno repo. The commit I'm observing the crash on is on the feature/breakingchanges branch, which requires .NET 11 Preview 5. First, build SamplesApp.Skia.netcoremobile.csproj:
git clone https://github.com/unoplatform/uno.git
cd uno
# note: commit 0ebec is on the feature/breakingchanges branch
git checkout 0ebec1e321fc5ff5c21ed166130f69241ddcfbb8
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-androidNext, launch an Android emulator. I use the emulator-uno script (expand the details block, below).
Note: I'm running this emulator on an arm64 macOS device, using the emulator Android SDK package, version 36.6.11, with an API-28 system image.
Once the emulator is running, capture adb logical output:
adb logcat > log.txtInstall and run the app:
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.MainActivityThe app launches, then crashes.
adb logcat contains:
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)
Note frame 14: ANativeWindow_fromSurface() is
uno/src/Uno.UI.Runtime.Skia.Android/Rendering/UnoSKVulkanView.cs
Lines 171 to 173 in 0ebec1e
| _nativeWindow = ANativeWindow_fromSurface( | |
| JNIEnv.Handle, | |
| surface.Handle); |
emulator-uno
#!/bin/sh
DEFAULT_API_LEVEL=28
DEFAULT_EMU_GPU_MODE=swiftshader_indirect
for arg in "$@" ; do
case "$arg" in
--help)
echo "Usage: [API_LEVEL=$DEFAULT_API_LEVEL] [EMU_GPU_MODE=$DEFAULT_EMU_GPU_MODE] $0"
echo "Launches Android emulator for API-\$API_LEVEL using \`-gpu\` mode \$EMU_GPU_MODE".
exit 0
esac
done
set -ex
TMP="$HOME/Developer/tmp"
export ANDROID_HOME="$HOME/android-toolchain/sdk"
export ANDROID_PREFS_ROOT="${ANDROID_PREFS_ROOT:=$HOME}"
CMDLINE_TOOLS="$ANDROID_HOME/cmdline-tools/19.0/bin"
EMU_GPU_MODE="${EMU_GPU_MODE:-$DEFAULT_EMU_GPU_MODE}"
API_LEVEL=${API_LEVEL:-$DEFAULT_API_LEVEL} # Skia may crash with https://github.com/unoplatform/uno/pull/22571
# API_LEVEL=${API_LEVEL:-34}
arch=`arch`
name=`uname`
if [ "$name" = Darwin -a "$arch" = 'i386' ]; then
AVD_ABI=x86_64
elif [ "$name" = Darwin -a "$arch" = "arm64" ]; then
AVD_ABI=arm64-v8a
fi
if [ "${AVD_ABI:-}" = "" ]; then
>&21 echo $0: could not determine emulator ABI.
exit 1
fi
AVD_NAME="xamarin_android_emulator"
IMAGE_NAME="system-images;android-$API_LEVEL;google_apis_playstore;$AVD_ABI"
AVD_CONFIG_FILE="$HOME/.android/avd/$AVD_NAME.avd/config.ini"
AVDMANAGER="$CMDLINE_TOOLS/avdmanager"
SDKMANAGER="$CMDLINE_TOOLS/sdkmanager"
if "$AVDMANAGER" list avd | grep "Name: $AVD_NAME" >/dev/null 2>&1; then
"$AVDMANAGER" delete avd -n "$AVD_NAME"
fi
"$SDKMANAGER" "$IMAGE_NAME"
echo "no" | "$AVDMANAGER" create avd \
-n "$AVD_NAME" --abi "$AVD_ABI" \
-k "$IMAGE_NAME" \
--sdcard 128M
# As seen in `android-uitest-run.sh` in Uno.Gallery!
echo "hw.cpu.ncore=3" >> "$AVD_CONFIG_FILE"
echo "vm.heapSize=256M" >> "$AVD_CONFIG_FILE"
exec "$ANDROID_HOME/emulator/emulator" -avd "$AVD_NAME" \
-skin 1280x800 -gpu "$EMU_GPU_MODE" -no-snapshot \
-no-boot-anim -noaudio -prop ro.debuggable=1 \
> "$TMP/loge.txt" 2>&1 Workaround 🛠️
No response
Renderer 🎨
- Skia
- Native
Affected platforms 📱💻🖥️
No response
Uno.Sdk version (and other relevant versions) 📦
No response
IDE version 🧑💻
No response
Anything else we need to know? 💬
No response
Activity
jonpryor commentedon Jul 30, 2026
As with all great heisenbugs, logging to know the state of things may changes things enough that it no longer crashes, if it works at all. Consider:
This message does not show for me at all. The app still crashes. If I add more logging or change the structure of the method, then I instead see the app launch, the screen "flash", and then the screen is blank/black.
adb logcatcontains:But focusing on the crash side of things, I can force the crash by replacing
surface.HandlewithIntPtr.Zero:The crash is otherwise "the same" as originally observed, with
ANativeWindow.fromSurface()in the native call stack.This allows us to create a working hypothesis. Consider
UnoSKVulkanView.InitializeVulkan():uno/src/Uno.UI.Runtime.Skia.Android/Rendering/UnoSKVulkanView.cs
Lines 161 to 187 in 0ebec1e
The hypothesis as follows:
var surface = holder.Surface(line 163) causes aSurfaceInvokerinstance to be created.surface.Handle(line 173) is the last use ofsurface, and "IL-wise in C#" what we'll have is:As line 173 is the last use of
surface, nothing prevents the GC from collecting thesurfaceinstance between thesurface.Handleinvocation and theANativeWindow_fromSurface()invocation. Meaning__hcould be invalid.A way to confirm this would be (lots of) luck and the enabling of GREF logging:
(Lots of luck would be necessary because the GREF logging may itself change program behavior enough that we don't see the crash anymore.)
However, enabling GREF logging fails differently:
Worse, trying to clear the
debug.mono.logsystem property doesn't fix things. A reboot of the emulator is required. (?!)Additionally, this weird behavior isn't easily reproducible:
adb shell setprop debug.mono.log grefin the new emulator doesn't trigger the same crashes. I guess the emulator was FUBAR.jonpryor commentedon Jul 30, 2026
Returning to the above hypothesis, if the problem is in fact that the GC is collecting the
surfaceinstance between accessing thesurface.Handleproperty and invokingANativeWindow_fromSurface(), then the fix is to tell the GC not to do that:With this change applied, I no longer see the crash. (I instead see a blank screen, with
adb logcatmentioningVK_ERROR_INCOMPATIBLE_DRIVER.)fix: extend Android Surface lifetime to avoid GC-related crash