Repository navigation
[CORE] Let backends raise typed Java exceptions for native errors at the JNI boundary - #13822
Open
felipepessoto wants to merge 4 commits into
Open
felipepessoto wants to merge 4 commits into
felipepessoto wants to merge 4 commits into
Conversation
…the JNI boundary Add JniExceptionMetadataRegistration, a scoped backend callback that describes a native exception as a Java class name, a reason and a diagnostic. JNI_METHOD_END asks it only when no Java exception is pending or carried, then constructs the class through its (byte[] reason, byte[] diagnostic) constructor after releasing the registry lock. A Java exception raised while constructing it stays pending; other failures keep the GlutenException fallback. The unwrapper registration keeps its API and behavior and shares the same null, duplicate and lifetime checks. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
…class A provider exception that isn't a std::exception no longer escapes the JNI boundary. It is logged, and the boundary keeps a pending Java throwable or otherwise the GlutenException fallback with the original diagnostic. The boundary also checks that the described class extends java.lang.Throwable before running its constructor or JNI Throw, and keeps the same fallback when it doesn't. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
…destroyed A provider exception was inspected and destroyed by handleJniException after describeBackendException had released the registry lock, so unregistering the provider could finish while its exception's what() or destructor still ran. Handle exceptions of any type from the callback inside the locked loop, and keep JNI class lookup, construction and Throw outside the lock. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
…handshake Unregistering waits until the boundary has inspected and released its own reference to a provider's exception. A provider that rethrows an exception it still references, such as through a std::exception_ptr, keeps that reference's lifetime. The concurrency test now waits until the unregistering thread has started before its bounded check, so a slow scheduler can't hide the race. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Contributor
Author
|
@zhztheplayer @rui-mo, could you review the JNI metadata hook? I would especially appreciate a look at how it preserves pending Java exceptions and keeps a provider registered while handling an exception from its callback. |
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues are reported for the JNI error-handling extension point.
No open issues 🎉
What changed in this PR
Adds a backend extension point to Gluten’s JNI error handling so native failures can become typed Java exceptions. This provides the hook for Velox cast-error translation in #13135; no backend registers a provider in this PR.
Changes:
- Introduces scoped metadata-provider registration and shares registry logic with exception unwrappers.
- Constructs typed Java throwables from UTF-8 byte arrays while preserving Java-exception precedence and fallback behavior.
- Adds native and real-JVM regression tests and documents the provider contract.
| File | Description |
|---|---|
gluten-arrow/src/test/java/org/apache/gluten/jni/JavaExceptionJniTest.java |
Tests embedded NULs, constructor failures, failed throws, and non-Throwable rejection. |
docs/developers/NewToGluten.md |
Documents metadata providers, lifetime guarantees, and fallback behavior. |
cpp/core/tests/JavaExceptionTest.cc |
Tests exception precedence, provider failures, scoped registration, and concurrent unregistration. |
cpp/core/tests/JavaExceptionJniTest.cc |
Adds native metadata-provider fixtures and throw-failure injection for JVM tests. |
cpp/core/jni/JniError.h |
Declares metadata and scoped registration APIs with their contracts. |
cpp/core/jni/JniError.cc |
Implements shared registries, metadata lookup, and typed Java exception construction. |
Review effort: Balanced
This branch has not been deployed
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.
Agent assessment
Self-reported assessment, not an independent review.
Assessed revision:
d85a1034099cdfc503fd09fefff17d85dec8f6b3↑ The new concurrency test fails against the previous handler, both when a provider's exception runs code in
what()and in its destructor. It passed 10/10 runs on this fix.↑ Other injected faults each fail the new tests: describing after a carried Java cause or with a pending throwable, a leaked registration, an escaping provider failure, and a missing or late
Throwablecheck.↑
JavaExceptionJniTestpasses 18/18 on Spark 3.5 and 4.1 under-Xcheck:jni, with no JNI warnings.GlutenJniExceptionSuitepasses 8/8 on Spark 4.1.↑ Formatting, license header, checkstyle, spotless and scalastyle checks pass. The repository's format and lint checks also pass on this revision.
↓ No backend registers a provider yet, so only tests exercise the typed path.
↓ The Velox runs in the fork are still queued for this revision.
What changes are proposed in this pull request?
This lets a backend raise its own Java exception type for a native error at the JNI return boundary, instead of the generic
GlutenException. This hook is derived from the JNI exception-handling work originally developed in #13133 (the combined reference PR).JniExceptionMetadataRegistration(ExceptionMetadataProvider)registers a callback for the backend library's lifetime. It has the same null check, duplicate check and scoped unregistration asJniExceptionUnwrapperRegistration. The two registrations now share that code, and the unwrapper's API and behavior are unchanged.std::exceptionas a JNI class name, a reason and a diagnostic, or returnsstd::nullopt. The strings are owned, and the callback must not call JNI.handleJniExceptionasks the providers only when no Java exception is pending and the error carries no Java throwable, so those keep precedence. After releasing the registry lock, it checks that the class extendsjava.lang.Throwable. It then constructs the class through its(byte[] reason, byte[] diagnostic)constructor and throws it. The byte arrays carry the UTF-8 bytes unchanged, including NULs.what()and destructor may be the backend's code. A provider that rethrows an exception it still references, for example through astd::exception_ptr, remains responsible for that reference.OutOfMemoryErrororNoClassDefFoundError, stays pending. Any other failure keeps theGlutenExceptionfallback with the original diagnostic. That includes a provider exception of any type, and a class that isn't aThrowable, whose constructor is never run.No backend registers a provider in this PR, so behavior doesn't change. #13135 uses this hook to raise a typed exception for Velox cast failures.
How was this patch tested?
New tests:
JniExceptionMetadataTest(C++, inJavaExceptionTest.cc):std::exception. A throwable left pending by a failing provider is kept.what(), or destroying one that isn't astd::exception. The test pauses that code on one thread and unregisters from another, with bounded waits.JavaExceptionJniTest(real JVM), with test-only classes:GlutenExceptionwith the original diagnostic.Throwablefalls back the same way, and its constructor doesn't run.I ran these in the
apache/gluten:centos-9-jdk17image, with the Velox version thatmainpins:JavaExceptionTest,JniExceptionMetadataTest,JavaExceptionAllocationTestandVeloxJavaExceptionTestcases.JavaExceptionJniTest(18 cases) on Spark 3.5 and 4.1, andGlutenJniExceptionSuite(8 cases) on Spark 4.1, all with-Xcheck:jni.dev/format-cpp-code.sh(clang-format 15.0.7),dev/format-scala-code.sh, the license header check, checkstyle, spotless and scalastyle.Was this patch authored or co-authored using generative AI tooling?
Generated-by: GitHub Copilot 1.0.115-7 (Claude Opus 5.5, model ID: claude-opus-5.5)