Repository navigation
[tests] Re-enable unregister peer test - #13700
Conversation
Context: #12031 JavaObjectTest.UnregisterFromRuntime compared the process-wide surfaced peer count before and after constructing and disposing a peer. Other threads can register or release unrelated peers meanwhile, so the test was ignored. Count only the surfaced-peer entries whose target is the peer under test, re-enable the test, and add coverage for an explicit UnregisterFromRuntime() call that removes the registration while the peer keeps its JNI reference until disposed. Co-authored-by: Copilot App <[email protected]>
Remove the additional lifetime checks; they are separate from fixing the flaky surfaced-peer count assertion. Co-authored-by: Copilot App <[email protected]>
Co-authored-by: Copilot App <[email protected]>
There was a problem hiding this comment.
🟡 Changes recommended
A failed assertion skips disposal of l, leaking a local JNI reference and leaving the thread-local reference count elevated in the shared test process.
1 open issue
What changed in this PR
This PR re-enables JavaObjectTest.UnregisterFromRuntime in the .NET for Android device test suite. It addresses the peer-count flakiness tracked in #12031 without changing runtime behavior or tackling the issue’s broader reference-count concerns.
Changes:
- Remove the test’s ignore attribute.
- Replace process-wide peer totals with identity-based counts for the peer under test, preserving registration and removal assertions.
| File | Description |
|---|---|
| tests/Mono.Android-Tests/Java.Interop-Tests/Java.Interop/JavaObjectTest.cs | Re-enable the unregister test and isolate its assertions from unrelated peer activity. |
Review effort: Balanced
| l = o.PeerReference.NewLocalRef (); | ||
| Assert.AreEqual (JniObjectReferenceType.Global, o.PeerReference.Type); | ||
| Assert.AreEqual (registeredCount+1, JniRuntime.CurrentRuntime.ValueManager.GetSurfacedPeers ().Count, "registeredCount+1 should match!"); | ||
| Assert.AreEqual (1, GetSurfacedEntryCount (o), "Constructed peer should be surfaced exactly once."); |
|
/review |
|
✅ Android PR Reviewer completed successfully!
|
There was a problem hiding this comment.
Code assessment: no blocking findings — 0 errors, 0 warnings, 1 non-blocking suggestion. CI verification is incomplete.
The peer-specific count addresses the ambient-registry race without weakening the exactly-once registration and post-disposal removal assertions. GetSurfacedPeers () returns a snapshot built under the registry lock, and ReferenceEquals correctly distinguishes managed peers rather than invoking Java-object equality. The existing PeekValue assertions preserve independent lookup checks. The reported concurrent-churn comparison supports the intended fix, although I did not independently rerun that experiment. The inline suggestion covers successful explicit unregistration of a live peer, which the current test does not exercise.
CI: All 39 checks returned for head 33b9df7ff59025b06a28432303491554d18d9af5 are completed/success, including the overall dotnet-android check linked to Azure build 1693170. Independent Azure build/timeline verification was blocked: the build query required login, and the timeline query failed during Azure CLI initialization. Consequently, I cannot confirm the full Azure result or non-gating device-test outcomes. Review covered the full changed file, its project configuration, and the runtime registration/disposal implementation; linked issue #12031 could not be fetched with the available GitHub read tools.
Generated by Android PR Reviewer for #13700 · copilot · gpt61 · 21.2 AIC · ⌖ 4.81 AIC · ⊞ 21K
Comment /review to run again
| l = o.PeerReference.NewLocalRef (); | ||
| Assert.AreEqual (JniObjectReferenceType.Global, o.PeerReference.Type); | ||
| Assert.AreEqual (registeredCount+1, JniRuntime.CurrentRuntime.ValueManager.GetSurfacedPeers ().Count, "registeredCount+1 should match!"); | ||
| Assert.AreEqual (1, GetSurfacedEntryCount (o), "Constructed peer should be surfaced exactly once."); |
There was a problem hiding this comment.
🤖 💡 Testing — Consider adding a companion case that calls o.UnregisterFromRuntime () while the peer is still live, then verifies GetSurfacedEntryCount (o) == 0, PeekValue (l) == null, and that o.PeerReference remains valid until disposal. This re-enabled test verifies removal through Dispose (), but its only call to UnregisterFromRuntime () is after disposal; a regression that removes the live method's RemovePeer (this) call would therefore still pass.
{Rule: Test edge cases and assert the behavior under test}
Dispose the local JNI reference even when an assertion fails, while keeping it available for the post-disposal peer lookup. Co-authored-by: Copilot App <[email protected]>
Fixes: #11690 Context: #12031 Context: #13700 `DoNotLeakWeakReferences` compared process-wide peer snapshots, so unrelated tests and GC could change the count or collect a snapshot target. Re-enable it by tracking only its own peer and waiting for the GC bridge before checking that the surfaced weak reference and peer lookup no longer retain it. The local- and global-reference accounting tests were inconclusive because their reflected JNI entry points no longer exist. Use the current direct JNI calls and clean up raw references without changing accounting. Local counts are thread-local; for global references, check events for the specific handle and test thread, with a tracked reference as a positive control.

Relates to #12031
JavaObjectTest.UnregisterFromRuntimewas disabled after its process-wide surfaced-peer count changed from an expected 68 to 71. Runtime activity on other threads can register unrelated peers even when test execution is serialized.Re-enable the test and count only entries targeting its own peer. This retains the existing registration and removal assertions without treating changes to the global registry as changes caused by the test. Dispose the test's local JNI reference in a
finallyso a failed assertion cannot leave it behind, while keeping it available for the post-disposal lookup. The other JNI reference-count concerns in #12031 remain outside this change.Validation
make prepare CONFIGURATION=Debug && make leeroy CONFIGURATION=Debug MSBUILD_ARGS='-m:4'— passed after initializing theexternal/android-api-docssubmodule../dotnet-local.sh build tests/Mono.Android-Tests/Java.Interop-Tests/Java.Interop-Tests.NET.csproj -c Debug --no-restore -v:minimal— passed, including after the exception-safe cleanup change and removal of the temporary category../dotnet-local.sh build -t:Install -c Debug tests/Mono.Android-Tests/Mono.Android-Tests/Mono.Android.NET-Tests.csproj -p:Device=emulator-5556 -p:IncludeCategories=JniPeerCountValidation -v:minimal— passed on a dedicated API 35 arm64 emulator.(cd tests/Mono.Android-Tests/Mono.Android-Tests && ../../../dotnet-local.sh test Mono.Android.NET-Tests.csproj --no-build -c Debug -p:Device=emulator-5556 -p:IncludeCategories=JniPeerCountValidation --report-trx --results-directory "$HOME/.copilot/session-state/73b59988-b6c9-4418-a19b-622e6a87ce0c/files/jni-linq-results")— passed: 1/1UnregisterFromRuntime. The category was added temporarily for selection and removed afterward.ADB_TARGET='-s emulator-5560' ./dotnet-local.sh build -t:Install -c Debug tests/Mono.Android-Tests/Mono.Android-Tests/Mono.Android.NET-Tests.csproj -p:Device=emulator-5560 -p:AdbTarget='-s emulator-5560' -p:IncludeCategories=JniLocalRefReview -v:minimal— passed on a separate API 35 arm64 emulator with a temporary selection category.(cd tests/Mono.Android-Tests/Mono.Android-Tests && ADB_TARGET='-s emulator-5560' ../../../dotnet-local.sh test Mono.Android.NET-Tests.csproj --no-build -c Debug -p:Device=emulator-5560 -p:AdbTarget='-s emulator-5560' -p:IncludeCategories=JniLocalRefReview --report-trx --results-directory "$HOME/.copilot/session-state/73b59988-b6c9-4418-a19b-622e6a87ce0c/files/jni-ref-review-final-results")— passed: 1/1UnregisterFromRuntime. The temporary category was removed afterward.jni-ref-review-results— passed: 2/2. With cleanup deliberately bypassed to simulate the old code, failures after acquiring the reference left the thread-local count elevated by one; with cleanup enabled the count returned to baseline, including a failure before reference creation. The probe explicitly disposed its captured reference and was removed afterward.Concurrent churn comparison
A temporary harness repeatedly ran the current test while a second worker in the same Android process synthetically created and explicitly disposed batches of 1–32 unrelated
JavaObjectpeers. At the original checkpoints, it evaluated the old process-wide count assertions as shadow checks and recorded their mismatches; failures of the current assertions still failed the run. There was no synchronization designed to force a mismatch. This is a matched, in-process comparison under synthetic churn, not a naturally failing test or a separate run of the unmodified original test; the instrumentation can affect timing.On dedicated API 35
arm64-v8aemulators:NativeAotRuntimeProviderand the native test library were loaded.For each run, 100 no-churn control iterations passed both sets of assertions. A preceding 10-second CoreCLR smoke run passed 2,480 current-test iterations and recorded old shadow mismatches in 33. These results demonstrate the ambient-count race under the exercised conditions; they do not establish coverage for every runtime/ABI or resolve the issue's other JNI-reference concerns.
./dotnet-local.sh build -t:Install -c Debug tests/Mono.Android-Tests/Mono.Android-Tests/Mono.Android.NET-Tests.csproj -p:Device=emulator-5556 -p:IncludeCategories=JniPeerStress -v:minimal— passed.(cd tests/Mono.Android-Tests/Mono.Android-Tests && ../../../dotnet-local.sh test Mono.Android.NET-Tests.csproj --no-build -c Debug -p:Device=emulator-5556 -p:IncludeCategories=JniPeerStress --report-trx --results-directory "$HOME/.copilot/session-state/73b59988-b6c9-4418-a19b-622e6a87ce0c/files/jni-stress-thirty-minute-results")— passed: 1/1 comparison test, with 395,082 successful inner iterations. Counters and duration were verified from the device-native TRX, with 29 minute-progress reports../dotnet-local.sh build tests/Mono.Android-Tests/Mono.Android-Tests/Mono.Android.NET-Tests.csproj -c Debug --no-restore -v:minimal— passed after byte-for-byte source restoration; two SourceLink warnings, zero errors../dotnet-local.sh build tests/Mono.Android-Tests/Mono.Android-Tests/Mono.Android.NET-Tests.csproj -c Release -p:TestsFlavor=NativeAOT -p:PublishAot=true -p:IncludeCategories=JniPeerStress15 -v:minimal— passed.ADB_TARGET=emulator-5560 ./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:IncludeCategories=JniPeerStress15 -p:Device=emulator-5560 -p:AdbTarget="-s emulator-5560" -v:minimal— passed.(cd tests/Mono.Android-Tests/Mono.Android-Tests && ADB_TARGET=emulator-5560 ../../../dotnet-local.sh test Mono.Android.NET-Tests.csproj --no-build -c Release -p:TestsFlavor=NativeAOT -p:PublishAot=true -p:IncludeCategories=JniPeerStress15 -p:Device=emulator-5560 -p:AdbTarget="-s emulator-5560" --report-trx --results-directory "$HOME/.copilot/session-state/73b59988-b6c9-4418-a19b-622e6a87ce0c/files/jni-naot-results")— passed: 1/1 comparison test, with 223,109 successful inner iterations. Counters and duration were verified from the device-native TRX.The experiment-only methods, instrumentation, and categories were removed, and the dedicated AVDs were stopped and deleted. The PR source is unchanged by the experiments.