Repository navigation
[Java.Interop] Keep JNI reference owners alive during calls - #12754
Conversation
Java.Interop.JniObjectReference contains a native handle, not a managed reference to its owner. Retain the source peer while Java.Interop.JavaPeerableExtensions.GetJniTypeName() looks up its Java class and JavaAs<TResult>() creates another managed peer. Also retain the throwable wrapper through JNI Throw in Android.Runtime.AndroidRuntime.RaisePendingException() and Java.Interop.JniEnvironment.Exceptions.Throw(Exception). These paths can create a temporary proxy that the original managed exception does not keep alive. Use GC.KeepAlive after the JNI work, matching existing owner-taking interop helpers. These changes address source-level lifetime gaps; they are not a demonstrated fix for the reported GetObjectClass crash. Context: https://github.com/dotnet/runtime/issues/132165#issuecomment-5624441077 Context: #12315 Co-authored-by: Copilot App <[email protected]>
There was a problem hiding this comment.
🟡 Changes recommended
The new GC.KeepAlive(...) guards should use try/finally to guarantee owner retention on exceptional paths, consistent with established Java.Interop JNI invocation patterns.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Pull request overview
This PR tightens managed lifetime guarantees around borrowed JNI handles by using GC.KeepAlive to ensure the managed owner (or proxy wrapper) isn’t collected while JNI is still consuming the native handle. This is in the Java.Interop/Mono.Android interop layer, where premature collection can surface as intermittent native crashes (e.g., around GetObjectClass/Throw).
Changes:
- Keep the
IJavaPeerableowner alive acrossGetJniTypeName()andJavaAs<TResult>()JNI usage. - Keep the throwable wrapper alive across JNI
Throwpaths in both Java.Interop and Mono.Android runtime code.
File summaries
| File | Description |
|---|---|
| src/Mono.Android/Android.Runtime/AndroidRuntime.cs | Keeps the Java throwable wrapper alive through RaisePendingException() JNI throw. |
| external/Java.Interop/src/Java.Interop/Java.Interop/JniEnvironment.Errors.cs | Keeps the proxy Java exception wrapper alive through JniEnvironment.Exceptions.Throw(Exception). |
| external/Java.Interop/src/Java.Interop/Java.Interop/JavaPeerableExtensions.cs | Keeps the peer owner alive across borrowed-handle use in GetJniTypeName() and JavaAs<TResult>(). |
Review details
Suppressed comments (1)
external/Java.Interop/src/Java.Interop/Java.Interop/JavaPeerableExtensions.cs:52
- 🤖 ❌ error JNI references —
GC.KeepAlive(self)should be in afinallyso the keep-alive is guaranteed even ifCreatePeer()throws while using the borrowedPeerReference. This matches the established pattern oftry { /* JNI */ } finally { GC.KeepAlive(self); }used throughout Java.Interop invoke helpers.
var peer = JniEnvironment.Runtime.ValueManager.CreatePeer (
ref r, JniObjectReferenceOptions.Copy,
targetType: typeof (TResult))
as TResult;
GC.KeepAlive (self);
- Files reviewed: 3/3 changed files
- Comments generated: 3
- Review effort level: Lite
|
/review |
|
✅ Android PR Reviewer completed successfully! Warning Firewall blocked 1 domainThe following domain was blocked by the firewall during workflow execution:
To allow these domains, add them to the network:
allowed:
- defaults
- "azcliprod.blob.core.windows.net"See Network Configuration for more information.
|
There was a problem hiding this comment.
The added GC.KeepAlive guards correctly protect the four modified JNI uses and match existing straight-line owner-lifetime patterns. One sibling diagnostic path still bypasses the guarded extension and retains the same lifetime hazard; see the inline comment.
CI build #1591605 is still in progress. The Java.Interop test jobs on Windows and macOS and the currently completed checks are passing; the Linux, macOS, and Windows build lanes have not finished.
Warning
Firewall blocked 1 domain
The following domain was blocked by the firewall during workflow execution:
azcliprod.blob.core.windows.net
To allow these domains, add them to the network.allowed list in your workflow frontmatter:
network:
allowed:
- defaults
- "azcliprod.blob.core.windows.net"See Network Configuration for more information.
Generated by Android PR Reviewer for #12754 · copilot · gpt56 · 97.7 AIC · ⌖ 8.72 AIC · ⊞ 21K
Comment /review to run again
Java.Interop.JniObjectReferencecontains a native handle, not a managed reference to its owner. CopyingPeerReferencetherefore does not keep the wrapper alive while JNI uses that handle.Retain the source peer through
Java.Interop.JavaPeerableExtensions.GetJniTypeName()andJavaAs<TResult>(). The latter can reachGetObjectClassduring peer creation before the new peer acquires its own JNI reference.Also retain the throwable wrapper through JNI
ThrowinAndroid.Runtime.AndroidRuntime.RaisePendingException()andJava.Interop.JniEnvironment.Exceptions.Throw(Exception). These methods can create a temporary proxy locally; retaining the original managed exception does not retain that proxy.The four guards use
GC.KeepAliveafter the JNI work, matching existing owner-taking helpers such asAndroidEnvironment.RaiseThrowable()andJNIEnv.ToLocalJniHandle(). No array operations or other raw-handle helpers are changed.These address source-level lifetime gaps identified while investigating #12765. They are not a demonstrated fix for the reported production
GetObjectClasscrash. No deterministic reproducer or GC-timing regression test is available, and device tests were not run because this worktree has no locally built Android SDK.Context: #12765 (comment)
Context: #12315