Skip to content

[tests] Re-enable skipped Mono.Android device tests - #13757

Open
simonrozsival wants to merge 1 commit into
mainfrom
simonrozsival-reenable-skipped-device-tests
Open

simonrozsival wants to merge 1 commit into
mainfrom
simonrozsival-reenable-skipped-device-tests

Conversation

@simonrozsival

@simonrozsival simonrozsival commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

Context: #11201
Context: #11202

Three Mono.Android.NET-Tests device tests were skipped on CoreCLR and/or NativeAOT even though their original causes are gone or fixable.

InflateCustomView_ShouldNotLeakGlobalRefs

Removing [Ignore] alone passed on CoreCLR but failed on NativeAOT: 302 extra global references after 100 inflations (before=20, after=322). The test asserted on the process-wide global reference count right after GC.Collect (), before the GC bridge had released unreachable peers. Under a stress harness with four background threads allocating Java objects, the same assertion also failed 92/200 rounds on CoreCLR (0/50 without the background load), because unrelated threads change the process-wide count.

The test now:

  • counts only the global references created while inflating, by following their lineage through an EventListener on the Java.Interop reference-tracing events, and
  • waits for the GC bridge to collect unreachable peers, retrying up to 10 collections before failing.

A positive control that leaks 3 global references per inflation still fails the test every time.

NewObjectArrayWithBadValues

JNIEnv.NewObjectArray (-1, …) passed the negative length straight to JNI, and ART aborted the process with JNI DETECTED ERROR IN APPLICATION: negative jsize: -1 in call to NewObjectArray. Both overloads now throw ArgumentOutOfRangeException before calling JNI. The test checks negative lengths, a null element class (ArgumentException), and a zero-length array, and releases its references.

java_lang_Object_Is_Java_Lang_Object

The CoreCLR Assert.Inconclusive ("Currently broken on CoreCLR") guard is removed. With managed typemaps, java/lang/Object maps to Java.Lang.Object on CoreCLR, and the test passes.

Validation

Release SDK and API 29 arm64 emulator:

  • make prepare CONFIGURATION=Release MSBUILD_ARGS='-m:4' and make leeroy CONFIGURATION=Release MSBUILD_ARGS='-m:4' — passed.
  • The seven initially investigated tests passed in 10 consecutive fresh-process runs on each of CoreCLR and NativeAOT, before the subsequent inflate-test stress rewrite.
  • Stress harness with four Java-allocating crosstalk threads (temporary, not committed): the rewritten inflate test had 0 failures in 600 CoreCLR rounds and 200 NativeAOT rounds.
  • Final full device suites — CoreCLR: 1125 passed, 1 skipped, 0 failed; NativeAOT: 1061 passed, 2 skipped, 0 failed. The remaining skips are runtime-specific tests (NativeAotStartupOffsetMatchesDevice, CoreClrStartupOffsetUsesDeviceTimeZone, GCBridgeRoundEmission).

The final full suites used the following build/install and instrumentation commands for each runtime (both builds and runs passed):

./dotnet-local.sh build -t:Install -c Release tests/Mono.Android-Tests/Mono.Android-Tests/Mono.Android.NET-Tests.csproj -p:TestsFlavor=CoreCLR -p:PublishAot=false -p:Optimize=true -p:Device=emulator-5554 -p:AdbTarget='-s emulator-5554' -v:quiet
adb -s emulator-5554 shell am instrument -w Mono.Android.NET_Tests/xamarin.android.runtimetests.TestInstrumentation
./dotnet-local.sh build -t:Install -c Release tests/Mono.Android-Tests/Mono.Android-Tests/Mono.Android.NET-Tests.csproj -p:TestsFlavor=NativeAOT -p:PublishAot=true -p:Optimize=true -p:Device=emulator-5554 -p:AdbTarget='-s emulator-5554' -v:quiet
adb -s emulator-5554 shell am instrument -w Mono.Android.NET_Tests/xamarin.android.runtimetests.TestInstrumentation

The post-rewrite stress runs and final full suites also included the separate GC.KeepAlive (self) fix in #13758. Before that fix, stress testing hit one intermittent "use of deleted global reference" crash. An excerpt of the crash log was preserved, but the exact cause is not proven.

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.

🟡 Changes recommended

The leak assertion can pass prematurely because it counts only global handles, missing tracked views temporarily held by weak handles during GC bridge rounds.

1 open issue
What changed in this PR

This PR restores three skipped Mono.Android device tests, including the custom-view leak regression test tracked by #11201 and disabled in #11202.

Changes:

  • Tracks inflation-related references and retries garbage collection instead of comparing process-wide reference counts.
  • Rejects negative JNI object-array lengths before calling JNI and expands invalid-input coverage.
  • Re-enables the CoreCLR java/lang/Object type-mapping assertion.
File Description
tests/​Mono.Android-Tests/​Mono.Android-Tests/​Java.Lang/​ObjectTest.cs Removes the CoreCLR test exclusion.
tests/​Mono.Android-Tests/​Mono.Android-Tests/​Java.Interop/​JnienvTest.cs Restores array validation tests and cleans up JNI references.
tests/​Mono.Android-Tests/​Mono.Android-Tests/​Android.Widget/​CustomWidgetTests.cs Restores leak testing with reference tracking and GC bridge retries.
src/​Mono.Android/​Android.Runtime/​JNIEnv.cs Adds and documents negative array-length validation.

Review effort: Balanced

public int LiveGlobalReferenceCount {
get {
lock (gate)
return live.Count (entry => entry.Value);

@PeppersMuse PeppersMuse left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Reviewed the leak-detection rewrite in this PR. One issue in the new InflatedGlobalReferences tracker:

public int LiveGlobalReferenceCount {
get {
lock (gate)
return live.Count (entry => entry.Value);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Leak assertion can pass prematurely: LiveGlobalReferenceCount drops to 0 while tracked views are temporarily weak during a GC bridge round.

I traced the event flow to confirm this (same area Copilot flagged — this adds the mechanism and the snapshot timing):

  • JavaMarshalGCBridge.TakeWeakGlobalReference creates the weak ref before deleting the global: the WeakGlobalCreated event records live[weakHandle] = false (via the live.ContainsKey (source) re-attachment, since source is the global handle), and the following GlobalDeleted event runs live.Remove (source), removing the global entry. So for the whole weak phase of a bridge round, a still-alive view contributes 0 to this count.
  • Promotion re-attaches it (TakeGlobalReference emits GlobalCreated with source = weak handle, so live[newGlobal] = true), so the dip is transient — but the retry loop snapshots leaked immediately after CollectUnreachablePeersAsync, whose trailing GC.Collect can start a new bridge round that is still in flight when the count is read. If that read lands in the weak phase, leaked <= maxLeaked exits the loop and the test passes despite a real leak. The positive control still fails every time because genuinely leaked refs are never freed, so this is a false-pass window, not a false-fail.
  • Counting weak handles as outstanding too (count all entries in live, not just Value == true) until their deletion is observed would close it, and it doesn't break the legitimate pass case: a truly collected view's weak handle is deleted (WeakGlobalDeleted → live.Remove (source)), so it still drops out of the count.

simonrozsival added a commit that referenced this pull request Nov 25, 2026
Context: #13757

`FinishCreateInstance` reads `self.PeerReference` before calling the nonvirtual Java constructor, but does not keep the managed peer reachable during that call. The GC/bridge can finalize or collect the peer and delete its global reference while JNI is still using it.

Keep `self` alive in a `finally` block around the constructor call, including when the call throws. Caller-owned JNI argument lifetimes remain unchanged.

Stress testing in the investigation for #13757, with four background threads allocating Java objects, hit one intermittent `JNI DETECTED ERROR IN APPLICATION: use of deleted global reference` crash on CoreCLR before the fix. An excerpt of the crash log was preserved, but the complete logcat was not saved and it does not establish that the crash occurred in `FinishCreateInstance`. There is no deterministic regression test.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants