Skip to content

[tests] Isolate JNI reference assertions - #13720

Merged
simonrozsival merged 2 commits into
mainfrom
simonrozsival-reenable-flaky-test
Nov 23, 2026
Merged

simonrozsival merged 2 commits into
mainfrom
simonrozsival-reenable-flaky-test

Conversation

@simonrozsival

@simonrozsival simonrozsival commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

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.

Validation

API 35 arm64 emulator (emulator-5556). All three restored tests passed in both full Release runs:

  • NativeAOT: 1,041 passed, 5 skipped, 0 failed.
  • CoreCLR: 1,103 passed, 6 skipped, 0 failed; one skip was due to unavailable DNS.

The Release SDK build passed:

make prepare CONFIGURATION=Release MSBUILD_ARGS='-m:4' && make leeroy CONFIGURATION=Release MSBUILD_ARGS='-m:4'

Final NativeAOT build/install passed with 39 warnings; test run passed:

./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-5556 -p:AdbTarget='-s emulator-5556' -p:IncludeCategories= -v:quiet
(
  cd tests/Mono.Android-Tests/Mono.Android-Tests
  ../../../dotnet-local.sh test Mono.Android.NET-Tests.csproj --no-build -c Release -p:TestsFlavor=NativeAOT -p:PublishAot=true -p:Optimize=true -p:Device=emulator-5556 -p:AdbTarget='-s emulator-5556' -p:IncludeCategories= --report-trx --results-directory /Users/simon/.copilot/session-state/58cfdf91-6ea7-4030-94fe-7692568631ec/files/jni-release-sdk-aot-results
)

Final CoreCLR build/install passed with 208 warnings; test run 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-5556 -p:AdbTarget='-s emulator-5556' -p:IncludeCategories= -v:quiet
(
  cd tests/Mono.Android-Tests/Mono.Android-Tests
  ../../../dotnet-local.sh test Mono.Android.NET-Tests.csproj --no-build -c Release -p:TestsFlavor=CoreCLR -p:PublishAot=false -p:Optimize=true -p:Device=emulator-5556 -p:AdbTarget='-s emulator-5556' -p:IncludeCategories= --report-trx --results-directory /Users/simon/.copilot/session-state/58cfdf91-6ea7-4030-94fe-7692568631ec/files/jni-release-sdk-coreclr-results
)

Ten-minute before/after stress comparison

On an API 35 arm64 emulator (emulator-5554), a temporary background thread continuously created batches of 100 unreachable Java peers (no explicit disposal) and called GC.Collect(). The original and fixed assertions ran alternately in the same process for at least 600 seconds per runtime, with no artificial delays between count reads.

  • CoreCLR, 600.149 seconds, 5,738 attempts per check: original weak-reference assertions failed 5,722 times; original global-count assertions failed 1,971 times. Fixed weak, fixed global, and local-reference control: zero failures each.
  • NativeAOT, 600.060 seconds, 6,052 attempts per check: original weak-reference assertions failed 6,000 times; original global-count assertions failed 1,725 times. Fixed weak, fixed global, and local-reference control: zero failures each.

CoreCLR created 17,837,600 unreachable peers and completed 35,403 bridge rounds; sampled global counts ranged from 19 to 5,422. NativeAOT created 15,259,900 peers and completed 35,374 bridge rounds; counts ranged from 21 to 7,229. Runtime identity was checked both through _AndroidRuntime and the on-device IsNativeAotRuntime flag.

The original weak test's assertions were copied unchanged, without its ignore attribute. For the original global test, only the unavailable reflective JNI entry points were replaced with direct equivalents and raw-reference cleanup was placed in finally; its process-wide count assertions were retained. Expected baseline failures were counted and logged rather than stopping the loop. Each harness run passed its final assertions requiring zero fixed-test failures and active reference-count churn.

The temporary harness was removed afterward, leaving the committed patch unchanged. Its patch, first failure stacks, minute-by-minute counters, final device logs, and TRX results are preserved in session artifacts (jni-ten-minute-*).

CoreCLR build/install and ten-minute comparison both passed (208 build warnings, 0 errors):

./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:Device=emulator-5554 -p:IncludeCategories=JniTenMinuteValidation -v:quiet
(
  cd tests/Mono.Android-Tests/Mono.Android-Tests
  ../../../dotnet-local.sh test Mono.Android.NET-Tests.csproj --no-build -c Release -p:TestsFlavor=CoreCLR -p:PublishAot=false -p:Device=emulator-5554 -p:IncludeCategories=JniTenMinuteValidation --report-trx --results-directory /Users/simon/.copilot/session-state/58cfdf91-6ea7-4030-94fe-7692568631ec/files/jni-ten-minute-coreclr-results
)

NativeAOT build/install and ten-minute comparison both passed (39 build warnings, 0 errors):

./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:IncludeCategories=JniTenMinuteValidation -v:quiet
(
  cd tests/Mono.Android-Tests/Mono.Android-Tests
  ../../../dotnet-local.sh test Mono.Android.NET-Tests.csproj --no-build -c Release -p:TestsFlavor=NativeAOT -p:PublishAot=true -p:Optimize=true -p:Device=emulator-5554 -p:IncludeCategories=JniTenMinuteValidation --report-trx --results-directory /Users/simon/.copilot/session-state/58cfdf91-6ea7-4030-94fe-7692568631ec/files/jni-ten-minute-nativeaot-results
)

Debug suite limitation

An unfiltered Debug/CoreCLR run aborted after 565 passes and 3 skips in ManagedExceptionRoundTripPreservesIdentity (False) with an LREF count is -1 assertion. The same abort reproduced with DoNotLeakWeakReferences temporarily ignored; the local/global reference tests had not run yet:

(
  cd tests/Mono.Android-Tests/Mono.Android-Tests
  ../../../dotnet-local.sh test Mono.Android.NET-Tests.csproj --no-build -c Debug -p:TestsFlavor=CoreCLR -p:Device=emulator-5556 -p:AdbTarget='-s emulator-5556' -p:IncludeCategories= --report-trx --results-directory /Users/simon/.copilot/session-state/58cfdf91-6ea7-4030-94fe-7692568631ec/files/jni-coreclr-final-results
)

simonrozsival and others added 2 commits November 23, 2026 10:43
Context: #12031

The surfaced-peer registry is process-wide; background runtime activity
can add or remove unrelated peers between snapshots, so asserting on the
total count was flaky. Count only entries for the peer under test, and
dispose the local reference in a finally block.

Co-authored-by: Copilot App <[email protected]>
Re-enable the weak peer and JNI reference accounting tests. Track only the test-owned peer or reference events instead of comparing process-wide state, and replace obsolete reflective JNI lookups with current direct APIs.

Undo the earlier UnregisterFromRuntime change; the same fix is already proposed in #13700.

Co-authored-by: Copilot App <[email protected]>
Copilot AI balanced review requested due to automatic review settings November 23, 2026 10:37

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.

🟢 Approval recommended

No active review issues remain for the JNI reference-test restoration and isolation changes.

No open issues 🎉

What changed in this PR

This PR restores three JNI reference tests and isolates their assertions from unrelated activity in the shared device-test process, addressing #11690 and part of #12031.

Changes:

  • Re-enables the weak-reference test, tracks only its own peer, and waits for GC bridge completion before checking collection.
  • Replaces obsolete reflected JNI calls with direct calls and exception-safe reference cleanup.
  • Checks global-reference accounting events by handle and test thread, with a tracked reference as a positive control.
File Description
tests/​Mono.Android-Tests/​Mono.Android-Tests/​Java.Interop/​JnienvTest.cs Restores peer-specific weak-reference assertions with GC bridge synchronization.
tests/​Mono.Android-Tests/​Java.Interop-Tests/​Java.Interop/​JniEnvironmentTests.cs Restores direct JNI reference tests and isolates global accounting checks.

Review effort: Balanced

@simonrozsival

Copy link
Copy Markdown
Member Author

/review

@github-actions

github-actions Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

✅ Android PR Reviewer completed successfully!

Generated by Android PR Reviewer for #13720

@github-actions github-actions Bot 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.

Code assessment: no blocking findings. 0 ❌ errors, 0 ⚠️ warnings, 1 💡 optional testing suggestion inline.

The direct JNI calls preserve the distinction between raw and accounted references, and the cleanup paths use the matching deletion mechanism. Filtering global-reference events by handle and emitting thread avoids process-wide counter drift; the tracked-reference positive control also prevents an inactive listener from making the negative assertions pass vacuously. The weak-reference test keeps peer construction on a terminated worker thread and uses the existing GC-bridge synchronization helper rather than assuming a fixed number of collections is sufficient.

⚠️ CI verification incomplete: all 39 GitHub checks returned success, including the aggregate dotnet-android check for build 1694781. Direct Azure build/timeline verification could not be completed: the build query required authentication, and the parallel timeline query failed during CLI initialization. I therefore cannot independently confirm the Azure build result or non-gating device-test outcomes. The Release/CoreCLR, NativeAOT, and stress-run results in the description are author-reported, not independently reproduced in this review.

Generated by Android PR Reviewer for #13720 · copilot · gpt61 · 34.2 AIC · ⌖ 7.94 AIC · ⊞ 20.3K
Comment /review to run again

}
}
Assert.IsNotNull (surfacedPeer, "The new peer should be surfaced.");
weakPeer = peer.PeerReference.NewWeakGlobalRef ();

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.

🤖 💡 Testing — Consider adding Assert.AreSame (peer, JniEnvironment.Runtime.ValueManager.PeekPeer (weakPeer)) here, while peer is still strongly reachable. The post-collection assertion only checks for null; this positive control would establish that the same weak JNI reference initially resolves to this registered peer, so a regression that makes weak-reference lookups always return null cannot satisfy that assertion unnoticed. This follows the useful positive-control pattern already added to the global-reference test.

{Rule: Test assertions must be specific; test edge cases}

@simonrozsival simonrozsival added the ready-to-review This PR is ready to review/merge, I think any CI failures are just flaky (ignorable). label Nov 23, 2026
@simonrozsival
simonrozsival merged commit d5f4792 into main Nov 23, 2026
39 checks passed
@simonrozsival
simonrozsival deleted the simonrozsival-reenable-flaky-test branch November 23, 2026 22:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-to-review This PR is ready to review/merge, I think any CI failures are just flaky (ignorable).

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[NativeAOT] Investigate failing DoNotLeakWeakReferences

3 participants