Repository navigation
[tests] Re-enable skipped Mono.Android device tests - #13757
simonrozsival wants to merge 1 commit into
Conversation
Co-authored-by: Copilot App <[email protected]>
There was a problem hiding this comment.
🟡 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/Objecttype-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
left a comment
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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.TakeWeakGlobalReferencecreates the weak ref before deleting the global: theWeakGlobalCreatedevent recordslive[weakHandle] = false(via thelive.ContainsKey (source)re-attachment, sincesourceis the global handle), and the followingGlobalDeletedevent runslive.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 (
TakeGlobalReferenceemitsGlobalCreatedwithsource= weak handle, solive[newGlobal] = true), so the dip is transient — but the retry loop snapshotsleakedimmediately afterCollectUnreachablePeersAsync, whose trailingGC.Collectcan start a new bridge round that is still in flight when the count is read. If that read lands in the weak phase,leaked <= maxLeakedexits 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 justValue == 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.
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.

Context: #11201
Context: #11202
Three
Mono.Android.NET-Testsdevice tests were skipped on CoreCLR and/or NativeAOT even though their original causes are gone or fixable.InflateCustomView_ShouldNotLeakGlobalRefsRemoving
[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 afterGC.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:
EventListeneron the Java.Interop reference-tracing events, andA positive control that leaks 3 global references per inflation still fails the test every time.
NewObjectArrayWithBadValuesJNIEnv.NewObjectArray (-1, …)passed the negative length straight to JNI, and ART aborted the process withJNI DETECTED ERROR IN APPLICATION: negative jsize: -1 in call to NewObjectArray. Both overloads now throwArgumentOutOfRangeExceptionbefore 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_ObjectThe CoreCLR
Assert.Inconclusive ("Currently broken on CoreCLR")guard is removed. With managed typemaps,java/lang/Objectmaps toJava.Lang.Objecton CoreCLR, and the test passes.Validation
Release SDK and API 29 arm64 emulator:
make prepare CONFIGURATION=Release MSBUILD_ARGS='-m:4'andmake leeroy CONFIGURATION=Release MSBUILD_ARGS='-m:4'— passed.NativeAotStartupOffsetMatchesDevice,CoreClrStartupOffsetUsesDeviceTimeZone,GCBridgeRoundEmission).The final full suites used the following build/install and instrumentation commands for each runtime (both builds and runs passed):
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.