Repository navigation
[release/11.0.1xx] [Java.Interop] Dispose unregistered peers normally - #13299
Merged
jonathanpeppers merged 1 commit intoOct 28, 2026
Merged
jonathanpeppers merged 1 commit into
jonathanpeppers merged 1 commit into
Conversation
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
Contributor
There was a problem hiding this comment.
🟢 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
approved these changes
Oct 28, 2026
jonathanpeppers
deleted the
simonrozsival-net-11-peer-disposal-backport
branch
October 28, 2026 18:28
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Backport the unregistered-disposal lifetime correction only from
c663cd110f
(#12772) directly onto
release/11.0.1xx. The broader peer-identity/activation reconciliation fromthat commit is intentionally excluded.
JniRuntime.JniValueManager.DisposePeerUnlessReferenced()currently callsthe 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
JavaObjectandJavaExceptionto release their native control blocks.Call the normal public
DisposePeer(value)path instead, preserving callbackentry 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 : JavaVMFixturefixture follows thisbranch's retained host-JVM and shared Android source-inclusion contracts and
does not duplicate
JavaPeerControlBlockTestsfrom #13294.Related coverage and public context:
#13292,
#13290, and
#13010.
Keep the tracker open until the companion fixes are accounted for.
Regression coverage
Both
JavaObjectandJavaExceptionare covered for native control-blockrelease, 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
JniReferenceLeakcases check actual JNI global-referencedeletion independently of the native allocation.
Local validation
Built this checkout's complete Release SDK from
release/11.0.1xxatc8c29a5810cfb0070aa82a348a8ec5cfaa8f670bplus this change:branch-pinned SDK
11.0.100-rtm.26524.110(reports11.0.100) and Androidpack
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.
make prepare CONFIGURATION=Release MSBUILD_ARGS='-v:minimal -m:4 -nr:false'bin/Release/dotnet/dotnet restore Microsoft.Android.slnx -p:Configuration=Release --packages "$NUGET_PACKAGES" --force -v:minimalmake leeroy CONFIGURATION=Release MSBUILD_ARGS='-v:minimal -m:4 -nr:false'./dotnet-local.sh build external/Java.Interop/tests/Java.Interop-Tests/Java.Interop-Tests.csproj -c Release -v:minimal./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-PositiveDevice 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:
publishAotcategorylabelfalseUnregisteredPeerDisposalUnregisteredPeerDisposal-CoreCLR-PositivefalseJniReferenceLeakJniReferenceLeak-CoreCLR-PositivetrueUnregisteredPeerDisposalUnregisteredPeerDisposal-NativeAOT-PositivetrueJniReferenceLeakJniReferenceLeak-NativeAOT-PositiveEach dedicated reference run contains this fixture's 4 new cases and
3 existing
JniTypeUtf8Testcases. 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.sofor CoreCLRor
libMono.Android.NET-Tests.sowithoutlibcoreclr.sofor NativeAOT.Total final positive results: 44.
The explicit install/test split is intentional: this branch's legacy
Mono.AndroidTools.AdbServer.Defaultdeployment 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
adbcommand. 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 rebuiltthe 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-Negativeproduced exactly 6 failures and 2 passes, with no skips. The device commands
above, with
publishAot=false,category=UnregisteredPeerDisposalandlabel=UnregisteredPeerDisposal-CoreCLR-Negative-Trimmable, produced exactly7 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.mdworkflow documentation, and in particular should include: