Skip to content

[CORE] Let backends raise typed Java exceptions for native errors at the JNI boundary - #13822

Open
felipepessoto wants to merge 4 commits into
apache:mainfrom
felipepessoto:felipepessoto-core-jni-error-metadata-hook
Open

felipepessoto wants to merge 4 commits into
apache:mainfrom
felipepessoto:felipepessoto-core-jni-error-metadata-hook

Conversation

@felipepessoto

@felipepessoto felipepessoto commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Agent assessment

Self-reported assessment, not an independent review.

Assessed revision: d85a1034099cdfc503fd09fefff17d85dec8f6b3

Dimension Assessment Rationale
Change confidence 9/10 ↑ All 25 native JNI exception tests pass, including 4 new boundary tests.
↑ 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 Throwable check.
↑ JavaExceptionJniTest passes 18/18 on Spark 3.5 and 4.1 under -Xcheck:jni, with no JNI warnings. GlutenJniExceptionSuite passes 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 as JniExceptionUnwrapperRegistration. The two registrations now share that code, and the unwrapper's API and behavior are unchanged.
  • The callback describes a native std::exception as a JNI class name, a reason and a diagnostic, or returns std::nullopt. The strings are owned, and the callback must not call JNI.
  • handleJniException asks 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 extends java.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.
  • If a provider throws, the boundary inspects and releases its reference to that exception before it releases the registry lock. So destroying a registration also waits until the boundary is done with that exception, whose what() and destructor may be the backend's code. A provider that rethrows an exception it still references, for example through a std::exception_ptr, remains responsible for that reference.
  • This is best effort. A Java exception raised while constructing the throwable, such as OutOfMemoryError or NoClassDefFoundError, stays pending. Any other failure keeps the GlutenException fallback with the original diagnostic. That includes a provider exception of any type, and a class that isn't a Throwable, 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++, in JavaExceptionTest.cc):
    • A pending throwable wins.
    • A carried Java throwable wins without asking the provider.
    • A provider failure keeps the original diagnostic, including one that isn't a std::exception. A throwable left pending by a failing provider is kept.
    • Unregistering waits while the boundary is still inspecting a provider's exception in what(), or destroying one that isn't a std::exception. The test pauses that code on one thread and unregisters from another, with bounded waits.
    • The registration is scoped.
  • JavaExceptionJniTest (real JVM), with test-only classes:
    • The typed exception keeps an embedded NUL.
    • A constructor failure stays pending.
    • A failed throw falls back to GlutenException with the original diagnostic.
    • A class that isn't a Throwable falls back the same way, and its constructor doesn't run.

I ran these in the apache/gluten:centos-9-jdk17 image, with the Velox version that main pins:

  • The 25 JavaExceptionTest, JniExceptionMetadataTest, JavaExceptionAllocationTest and VeloxJavaExceptionTest cases.
  • JavaExceptionJniTest (18 cases) on Spark 3.5 and 4.1, and GlutenJniExceptionSuite (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)

…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>
felipepessoto and others added 2 commits November 27, 2026 00:56
…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>
@felipepessoto
felipepessoto marked this pull request as ready for review November 27, 2026 16:45
Copilot AI balanced review requested due to automatic review settings November 27, 2026 16:45
@felipepessoto

Copy link
Copy Markdown
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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants