Skip to content

[release/11.0.1xx] [Java.Interop] Dispose unregistered peers normally - #13299

Merged
jonathanpeppers merged 1 commit into
release/11.0.1xxfrom
simonrozsival-net-11-peer-disposal-backport
Oct 28, 2026
Merged

jonathanpeppers merged 1 commit into
release/11.0.1xxfrom
simonrozsival-net-11-peer-disposal-backport

Conversation

@simonrozsival

Copy link
Copy Markdown
Member

Backport the unregistered-disposal lifetime correction only from
c663cd110f
(#12772) directly onto
release/11.0.1xx. The broader peer-identity/activation reconciliation from
that commit is intentionally excluded.

JniRuntime.JniValueManager.DisposePeerUnlessReferenced() currently calls
the private JNI-reference deletion helper for an unregistered peer. That
bypasses IJavaPeerable.Disposed() / Dispose(true), clears the JNI reference,
and suppresses finalization without setting the disposed state that permits
JavaObject and JavaException to release their native control blocks.
Call the normal public DisposePeer(value) path instead, preserving callback
entry with a valid JNI reference and normal registry/reference-tracking cleanup.

This is the companion to #13294:
that PR backports the finalization correction; this PR closes the separate
unregistered-disposal gap. Neither supersedes the other. The new
JavaUnregisteredPeerDisposalTests : JavaVMFixture fixture follows this
branch's retained host-JVM and shared Android source-inclusion contracts and
does not duplicate JavaPeerControlBlockTests from #13294.

Related coverage and public context:
#13292,
#13290, and
#13010.
Keep the tracker open until the companion fixes are accounted for.

Regression coverage

Both JavaObject and JavaException are covered for native control-block
release, exactly one Dispose(true) callback with a valid peer reference,
registered-peer no-op behavior, unregistered aliases that leave the registered
winner intact, throwing callbacks followed by a usable-reference cleanup retry,
and repeated disposal without double-free. Android additionally covers the exact
Rect.UnregisterFromRuntime(); Rect.DisposeUnlessReferenced(); sequence.
Four separately warmed JniReferenceLeak cases check actual JNI global-reference
deletion independently of the native allocation.

Local validation

Built this checkout's complete Release SDK from
release/11.0.1xx at c8c29a5810cfb0070aa82a348a8ec5cfaa8f670b plus this change:
branch-pinned SDK 11.0.100-rtm.26524.110 (reports 11.0.100) and Android
pack 37.2.1. All mutable SDK/JDK/NDK, NuGet restore/package/cache, Gradle,
ccache, CLI-home and AVD paths were explicitly worktree-local. No other
checkout's compiled SDK was used.

Commands Result
make prepare CONFIGURATION=Release MSBUILD_ARGS='-v:minimal -m:4 -nr:false' Passed
bin/Release/dotnet/dotnet restore Microsoft.Android.slnx -p:Configuration=Release --packages "$NUGET_PACKAGES" --force -v:minimal Passed
make leeroy CONFIGURATION=Release MSBUILD_ARGS='-v:minimal -m:4 -nr:false' Passed with the original helper for the negative control, and again after restoring the correction; includes extra API levels and local workload configuration
./dotnet-local.sh build external/Java.Interop/tests/Java.Interop-Tests/Java.Interop-Tests.csproj -c Release -v:minimal Passed
./dotnet-local.sh test external/Java.Interop/bin/TestRelease-net10.0/Java.Interop-Tests.dll --filter 'FullyQualifiedName~JavaUnregisteredPeerDisposalTests' --logger 'trx;LogFileName=positive-host.trx' --results-directory bin/TestRelease/TestResults/UnregisteredPeerDisposal-Host-Positive 12 passed, 0 failed, 0 skipped, host JVM

Device commands below were run on an owned Android 15/API 35 ARM64 emulator
(emulator-5586) through an isolated ADB socket (tcp:localhost:5039).
For each row, the test app was cleaned, signed, installed and tested with the
same properties:

# Common arguments, with the PublishAot and IncludeCategories values from the table.
properties=(
  -p:RuntimeIdentifier=android-arm64
  -p:Device=emulator-5586
  -p:UseMonoRuntime=false
  -p:AndroidTypeMapImplementation=trimmable
  "-p:IncludeCategories=$category"
  "-p:AndroidNdkDirectory=$AndroidNdkDirectory"
  "-p:PublishAot=$publishAot"
)
project=tests/Mono.Android-Tests/Mono.Android-Tests/Mono.Android.NET-Tests.csproj

./dotnet-local.sh clean "$project" -c Release "${properties[@]}" \
  -p:BuildProjectReferences=false -v:minimal
./dotnet-local.sh build -t:SignAndroidPackage -c Release "$project" \
  "${properties[@]}" -v:minimal
"$AndroidSdkDirectory/platform-tools/adb" -s emulator-5586 install -r -t \
  "$PWD/bin/TestRelease/net11.0-android/android-arm64/Mono.Android.NET_Tests-Signed.apk"
(
  cd tests/Mono.Android-Tests/Mono.Android-Tests
  ../../../dotnet-local.sh test Mono.Android.NET-Tests.csproj --no-build \
    -c Release "${properties[@]}" \
    -p:DeployToDeviceDependsOnTargets=_EnsureDeviceBooted \
    --report-trx --results-directory "../../../bin/TestRelease/TestResults/$label"
)
Runtime / publishAot category label Result
CoreCLR / false UnregisteredPeerDisposal UnregisteredPeerDisposal-CoreCLR-Positive 9 passed, 0 failed, 0 skipped
CoreCLR / false JniReferenceLeak JniReferenceLeak-CoreCLR-Positive 7 passed, 0 failed, 0 skipped
NativeAOT / true UnregisteredPeerDisposal UnregisteredPeerDisposal-NativeAOT-Positive 9 passed, 0 failed, 0 skipped
NativeAOT / true JniReferenceLeak JniReferenceLeak-NativeAOT-Positive 7 passed, 0 failed, 0 skipped

Each dedicated reference run contains this fixture's 4 new cases and
3 existing JniTypeUtf8Test cases. Parsed the actual TRX identities/counts,
checked zero skips, and compared the identical fixture cases between CoreCLR
and NativeAOT. Verified the signed APKs contain libcoreclr.so for CoreCLR
or libMono.Android.NET-Tests.so without libcoreclr.so for NativeAOT.
Total final positive results: 44.

The explicit install/test split is intentional: this branch's legacy
Mono.AndroidTools.AdbServer.Default deployment path hard-codes port 5037,
while the newer CLI runner honors the isolated ADB socket. The deployment
dependency override only avoids redeploying an APK already installed by the
explicit adb command. The trimmable lane explicitly roots both test assemblies;
an earlier default LLVM-IR/full-trim attempt removed the fixture and ran zero
tests, so that attempt is not counted. Earlier disconnected/default-port
deployment errors likewise never reached assertions and are not validation.

Negative control and limits

Temporarily retained only the original DisposePeer(h, value) call and rebuilt
the SDK. The focused host command
./dotnet-local.sh test external/Java.Interop/bin/TestRelease-net10.0/Java.Interop-Tests.dll --filter 'FullyQualifiedName~JavaUnregisteredPeerDisposalTests&TestCategory!=JniReferenceLeak' --logger 'trx;LogFileName=negative-host.trx' --results-directory bin/TestRelease/TestResults/UnregisteredPeerDisposal-Host-Negative
produced exactly 6 failures and 2 passes, with no skips. The device commands
above, with publishAot=false, category=UnregisteredPeerDisposal and
label=UnregisteredPeerDisposal-CoreCLR-Negative-Trimmable, produced exactly
7 failures and 2 passes, with no skips: four missing managed-disposal
callbacks, two missing expected callback exceptions, and the retained Rect
control-block pointer. The two registered-peer no-op cases passed on the old
code. Restored the production source byte-for-byte before rebuilding and
running every final positive result reported above.

Coverage is limited to the host JVM and Android 15 ARM64 CoreCLR/NativeAOT with
the trimmable typemap; no other ABI, MonoVM or default LLVM-IR execution is
claimed. #13294 was not applied for validation. This PR therefore makes no
claim that natural-finalization retention is fixed without that separate
backport. CI is not claimed green, and no merge or auto-merge is requested.


Pull Request
title and
description
should follow the
commit-messages.md workflow documentation, and in particular should include:

  • Useful description of why the change is necessary.
  • Links to issues fixed
  • Unit tests

Backport only the unregistered-disposal correction from c663cd1
(#12772), without its peer-identity and activation changes.

DisposePeerUnlessReferenced() bypasses Disposed()/Dispose(true) when it
calls the private reference-deletion helper.  JavaObject and
JavaException then retain their native control blocks after the JNI
reference is cleared and finalization is suppressed.  Use the public
DisposePeer(value) path so managed disposal precedes normal cleanup.

Add a distinct JavaVMFixture-based fixture for this branch's host JVM
and Android inclusion paths.  Cover valid-reference callback entry,
native-block release, registered no-ops, alias winners, throwing
callbacks and retry, repeated disposal, exact Rect disposal, and
separate warmed global-reference deletion checks.

The source-built Release SDK passes 12 host-JVM tests and 9 ordinary
plus 7 dedicated GREF tests on each of CoreCLR and NativeAOT.  The
original helper produces exactly 6 host and 7 device failures; restore
the production correction before repeating positive validation.

Complement the finalization backport in #13294 without duplicating
its implementation or JavaPeerControlBlockTests fixture.

Refs #13290, #13292, #13294

Co-authored-by: Copilot App <[email protected]>
Copilot-Session: 97270e0d-9fba-4a8b-8a04-9b7c25e1cd21
Copilot AI balanced review requested due to automatic review settings October 28, 2026 15:51

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

The unregistered-peer disposal backport has no active review issues.

No open issues 🎉

What changed in this PR

Backports the unregistered-peer disposal correction to release/11.0.1xx, routing cleanup through normal managed disposal so native control blocks can be released. This complements #13294’s finalization fix without including the broader peer-identity changes from #12772.

Changes:

  • Calls public DisposePeer(value) for unregistered peers.
  • Adds regression coverage for callbacks, control-block release, registered peers, aliases, exception retries, repeated disposal, and JNI reference deletion.
  • Documents the unregistered-disposal lifecycle and retry behavior.
File Description
external/​Java.Interop/​tests/​Java.Interop-Tests/​Java.Interop/​JavaUnregisteredPeerDisposalTests.cs Adds shared host/Android disposal tests and the Android-specific Rect regression.
external/​Java.Interop/​src/​Java.Interop/​Java.Interop/​JniRuntime.JniValueManager.cs Routes unregistered peers through normal disposal.
external/​Java.Interop/​src/​Java.Interop/​Documentation/​Java.Interop/​IJavaPeerable.xml Documents cleanup, registered-wrapper preservation, and callback-failure retries.

Review effort: Balanced

@jonathanpeppers
jonathanpeppers merged commit 7d4af78 into release/11.0.1xx Oct 28, 2026
45 checks passed
@jonathanpeppers
jonathanpeppers deleted the simonrozsival-net-11-peer-disposal-backport branch October 28, 2026 18:28
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