rubicon-java
rubicon-java copied to clipboard
If a Java call triggers an exception, clear the exception so JNI can continue operation
Describe the bug If there's a Java exception, we need to at least clear the exception so JNI knows we're aware of it.
When do we so, it is smart to print its stacktrace or otherwise do something useful for debugging. It might be sensible to raise a Python exception.
To Reproduce Steps to reproduce the behavior:
- Run some Python code that triggers an unhandled Java exception
- Immediately after that, run some more Python code that does operations against JNI
Expected behavior
Clear the JNI exception state. Additionally, perhaps see a Java stacktrace or other information about what caused the exception, perhaps in the form of a Python exception.
Actual behavior
On Android, the process gets killed, and this gets logged. Note the text about a "pending exception."
I/DEBUG ( 1143): signal 6 (SIGABRT), code -6 (SI_TKILL), fault addr --------
I/DEBUG ( 1143): Abort message: 'art/runtime/check_jni.cc:65] JNI DETECTED ERROR IN APPLICATION: JNI NewGlobalRef called with pending exception 'java.lang.ClassNotFoundException' thrown in unknown throw location'
I/DEBUG ( 1143): eax 00000000 ebx 00000a47 ecx 00000a47 edx 00000006
I/DEBUG ( 1143): esi b77cfc48 edi 0000000b
I/DEBUG ( 1143): xcs 00000073 xds 0000007b xes 0000007b xfs 00000007 xss 0000007b
I/DEBUG ( 1143): eip b755d2e6 ebp 00000a47 esp bf96dcd0 flags 00000286
Additional context
https://github.com/beeware/briefcase/issues/538
See e.g. https://www.developer.com/java/data/exception-handling-in-jni.html and https://docs.oracle.com/en/java/javase/13/docs/specs/jni/functions.html#exceptioncheck to check for these exceptions.