Skip to content

fix(java/driver/jni): check for pending exceptions more thoroughly - #4398

Merged
lidavidm merged 1 commit into
apache:mainfrom
lidavidm:jni-exception-check
Jun 23, 2026
Merged

fix(java/driver/jni): check for pending exceptions more thoroughly#4398
lidavidm merged 1 commit into
apache:mainfrom
lidavidm:jni-exception-check

Conversation

@lidavidm

Copy link
Copy Markdown
Member

After JNI calls that can leave a pending Java exception (GetArrayLength, GetObjectArrayElement, GetIntArrayRegion, GetByteArrayRegion, NewByteArray, SetByteArrayRegion) and are followed by further JNI or native ADBC work, check env->ExceptionCheck() and return the function's existing error default, letting the pending exception propagate to Java rather than performing more work or raising a second exception.

Generated-by: Claude Opus 4.8 noreply@anthropic.com

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR strengthens JNI error handling in the Java JNI driver by explicitly checking for pending Java exceptions after specific JNI calls, and returning early to let the original Java exception propagate instead of continuing native work (which could trigger additional failures or secondary exceptions).

Changes:

  • Added env->ExceptionCheck() short-circuit returns after array length and element access in openDatabase and metadata helpers.
  • Added exception checks around byte-array allocation and population (NewByteArray, SetByteArrayRegion) in *GetOptionBytes paths.
  • Added exception checks after reading array contents (GetArrayLength, Get*ArrayRegion) in *SetOptionBytes paths.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread java/driver/jni/src/main/cpp/jni_wrapper.cc
@lidavidm
lidavidm marked this pull request as ready for review June 15, 2026 13:06
@lidavidm
lidavidm marked this pull request as draft June 15, 2026 13:06
After JNI calls that can leave a pending Java exception (GetArrayLength,
GetObjectArrayElement, GetIntArrayRegion, GetByteArrayRegion, NewByteArray,
SetByteArrayRegion) and are followed by further JNI or native ADBC work,
check env->ExceptionCheck() and return the function's existing error
default, letting the pending exception propagate to Java rather than
performing more work or raising a second exception.

Generated-by: Claude Opus 4.8 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated no new comments.

@lidavidm
lidavidm marked this pull request as ready for review June 21, 2026 23:28
@lidavidm
lidavidm requested review from amoeba and zeroshade June 21, 2026 23:28

@zeroshade zeroshade left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general looks good to me, but I don't know JNI behavior well enough so I have a single question.

jclass nativeHandleKlass = RequireImplClass(env, "NativeDatabaseHandle");
jmethodID nativeHandleCtor = RequireMethod(env, nativeHandleKlass, "<init>", "(J)V");
const jsize num_params = env->GetArrayLength(parameters);
if (env->ExceptionCheck()) return nullptr;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

returning null is the correct behavior as opposed to throwing the exception or otherwise explicitly propagating it?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The exception is already thrown on the Java side. Throwing a C++ exception is unrelated to what happens in Java.

@lidavidm
lidavidm merged commit 0e1d0be into apache:main Jun 23, 2026
19 of 21 checks passed
@lidavidm
lidavidm deleted the jni-exception-check branch June 23, 2026 22:14
@lidavidm lidavidm added this to the ADBC Libraries 24 milestone Jul 22, 2026
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