Skip to content

[Mono.Android] Partially constructed Throwable leaks GREF when SetJavaStackTrace fails #13066

Closed

Description

@austinaviv

Description

When constructing a Java.Lang.Throwable wrapper or activating one through the typemap with DoNotRegister, if SetJavaStackTrace throws an exception (such as IllegalStateException or other runtime exceptions during printStackTrace), the partially constructed peer instance is aborted without calling Dispose().

This causes the global reference allocated by Construct to leak permanently, and in cases where DoNotRegister is not set, leaves the partially constructed wrapper registered in ValueManager.

This issue causes Java.InteropTests.TransferredReferenceTests.RuntimeExceptionLookupFailureReleasesInput(4, ...) to fail on macOS emulator test runs:

The partially constructed wrapper's own copies must be disposed separately. Expected: <empty> But was: < [29354, " at Java.InteropTests.TransferredReferenceTests.ReferenceObserver.CreateGlobalReference(JniObjectReference reference) at Java.Interop.JniObjectReference.NewGlobalRef() at Microsoft.Android.Runtime.TrimmableTypeMapValueManager.ConstructPeerCore(IJavaPeerable peer, JniObjectReference& reference, JniObjectReferenceOptions options) at Java.Interop.JniRuntime.JniValueManager.ConstructPeer(IJavaPeerable peer, JniObjectReference& reference, JniObjectReferenceOptions options) at Java.Interop.JavaException.Construct(JniObjectReference& reference, JniObjectReferenceOptions options) at Java.Lang.Throwable.SetHandle(IntPtr value, JniHandleOwnership transfer)

Steps to Reproduce

Run RuntimeExceptionLookupFailureReleasesInput(4, DoNotTransfer) or TransferLocalRef / TransferGlobalRef on the emulator test suite.

Expected Behavior

If SetJavaStackTrace throws inside Throwable.SetHandle, the wrapper should be disposed before re-throwing so allocated GREFs are released and uncompleted peers are removed from the runtime value manager.

Actual Behavior

SetJavaStackTrace is not wrapped in a try/catch in Throwable.SetHandle, so an exception during stack trace capture leaks the allocated GREF and fails stage 4 reference assertions.

Activity

  1. added
    needs-triage
    on Oct 11, 2026
  2. added 2 commits that reference this issue on Oct 11, 2026
    60a00fd
    2eaa58e
  3. added a commit that references this issue on Oct 12, 2026
    01e70c2
  4. locked and limited conversation to collaborators on Nov 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs-triage

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions