felipepessoto commented on code in PR #13560: URL: https://github.com/apache/gluten/pull/13560#discussion_r4544219743
########## cpp/core/jni/JavaException.cc: ########## @@ -0,0 +1,181 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "JavaException.h" + +#include <glog/logging.h> + +#include <stdexcept> +#include <string> + +namespace gluten { +namespace { + +// The describer class and result need two locals; reserve one spare slot. +constexpr jint kDescriptionLocalCapacity = 3; + +class LocalFrame { + public: + explicit LocalFrame(JNIEnv* env) : env_(env), pushed_(env->PushLocalFrame(kDescriptionLocalCapacity) == JNI_OK) {} + + ~LocalFrame() { + if (pushed_) { + env_->PopLocalFrame(nullptr); + } + } + + bool pushed() const { + return pushed_; + } + + private: + JNIEnv* env_; + bool pushed_; +}; + +std::string describe(JNIEnv* env, jthrowable throwable) { + const std::string prefix = "Error during calling Java code from native code: "; + const auto unavailable = [&](const char* reason) { + // Describing an exception must not replace the throwable we are transporting. + env->ExceptionClear(); + return prefix + "Java stack trace unavailable (" + reason + ")"; + }; + LocalFrame frame(env); + if (!frame.pushed()) { + return unavailable("PushLocalFrame failed"); + } + auto describer = env->FindClass("org/apache/gluten/exception/JniExceptionDescriber"); + if (describer == nullptr) { + return unavailable("JniExceptionDescriber lookup failed"); + } + auto method = env->GetStaticMethodID(describer, "describe", "(Ljava/lang/Throwable;)Ljava/lang/String;"); + if (method == nullptr) { + return unavailable("describe method lookup failed"); + } + auto description = static_cast<jstring>(env->CallStaticObjectMethod(describer, method, throwable)); + if (env->ExceptionCheck() || description == nullptr) { + return unavailable("describe method failed"); + } + const auto release = [&](const char* chars) { env->ReleaseStringUTFChars(description, chars); }; + std::unique_ptr<const char, decltype(release)> chars(env->GetStringUTFChars(description, nullptr), release); + if (!chars) { + return unavailable("GetStringUTFChars failed"); + } + return prefix + chars.get(); +} + +} // namespace + +struct JavaException::State { + State(JNIEnv* env, jthrowable throwable) { + if (throwable == nullptr) { + throw std::invalid_argument("JavaException requires a non-null throwable"); + } + if (env->GetJavaVM(&vm) != JNI_OK) { + throw std::runtime_error("Unable to get JavaVM for Java exception"); + } + ref = static_cast<jthrowable>(env->NewGlobalRef(throwable)); + if (ref == nullptr) { + throw std::runtime_error("Unable to create global reference for Java exception"); + } + } + + ~State() { + JNIEnv* env = nullptr; + auto status = vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_8); + const bool detached = status == JNI_EDETACHED; + if (detached) { + status = vm->AttachCurrentThreadAsDaemon(reinterpret_cast<void**>(&env), nullptr); + } + if (status != JNI_OK) { + LOG(ERROR) << "Unable to attach thread to release Java exception global reference: " << status; + return; + } + env->DeleteGlobalRef(ref); + // Only detach if this destructor attached the thread; never detach a caller's JNI/HDFS thread. + if (detached && vm->DetachCurrentThread() != JNI_OK) { + LOG(ERROR) << "Unable to detach thread after releasing Java exception global reference"; + } + } + + JavaVM* vm{nullptr}; + jthrowable ref{nullptr}; + std::string message; +}; + +JavaException::JavaException(JNIEnv* env, jthrowable throwable) : state_(std::make_shared<State>(env, throwable)) { + try { + state_->message = describe(env, throwable); + } catch (const std::exception&) { + // Keep the captured throwable even if diagnostics fail. what() must not allocate a fallback. + env->ExceptionClear(); + } +} + +const char* JavaException::what() const noexcept { + return state_->message.empty() + ? "Error during calling Java code from native code: Java stack trace unavailable (native diagnostic failure)" + : state_->message.c_str(); +} + +void JavaException::throwToJava(JNIEnv* env) const noexcept { + if (env->Throw(state_->ref) != JNI_OK) { + LOG(ERROR) << "Unable to restore original Java exception"; + } +} + +bool JavaException::throwNestedToJava(JNIEnv* env, const std::exception& error) noexcept { + const auto* nested = dynamic_cast<const std::nested_exception*>(&error); + if (nested == nullptr || nested->nested_ptr() == nullptr) { + return false; + } + try { + std::rethrow_exception(nested->nested_ptr()); + } catch (const JavaException& original) { + original.throwToJava(env); + return true; + } catch (const std::exception& cause) { + return throwNestedToJava(env, cause); + } catch (...) { + // Not a Java exception. Leave the enclosing native error to the existing JNI fallback. + return false; + } +} + +void JavaException::check(JNIEnv* env) { + if (!env->ExceptionCheck()) { + return; + } + auto throwable = env->ExceptionOccurred(); + env->ExceptionClear(); + const auto release = [env](jthrowable ref) { env->DeleteLocalRef(ref); }; + std::unique_ptr<_jthrowable, decltype(release)> local(throwable, release); + // Guard construction only: successful carrier propagation must leave JNI clear. + auto error = [env, throwable]() { + try { + return JavaException(env, throwable); + } catch (...) { + if (!env->ExceptionCheck() && env->Throw(throwable) != JNI_OK) { Review Comment: Added a nonallocating `JniEntryGuard` to `JNI_METHOD_START`. Failed carrier construction now restores or retains pending Java state only when the current thread has a guarded JNI entry. Otherwise it clears the pending exception before propagating the native failure, without detaching the worker. Successfully captured carriers still use shared global references and remain transferable across threads. The regressions reproduce both initial-state and global-reference allocation failures on native workers. They now verify that the same attached worker can execute another Java callback, while guarded JNI entries still preserve the original or newer pending exception. Nested Java reentry and cross-library scope checks also pass. ########## docs/developers/NewToGluten.md: ########## @@ -94,6 +94,46 @@ To format Java/Scala code using the [Spotless](https://github.com/diffplug/spotl ./dev/format-scala-code.sh ``` +### Java exceptions crossing native code + +JNI callbacks must call `checkException(env)` after invoking Java. It throws +`gluten::JavaException`, which owns a shared global reference to the original Java +throwable. `JNI_METHOD_END` restores that same object with JNI `Throw`, retaining Review Comment: Yes, the existing Java-side wrapping is intended to stay. Updated the documentation and PR description to make the distinction explicit: the same throwable is restored at the JNI return boundary, while public `hasNext()`/`next()` may wrap an `Exception` in `GlutenException` or the existing schema-error translation, preserving the original in the cause chain. Callers therefore cannot assume the original is the top-level class or `SparkThrowable`. `ClosableIterator` and `ColumnarBatchOutIterator` behavior is unchanged; the preservation guarantee is not a change to their wrapping policy. ########## cpp/core/jni/JavaException.cc: ########## @@ -0,0 +1,181 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "JavaException.h" + +#include <glog/logging.h> + +#include <stdexcept> +#include <string> + +namespace gluten { +namespace { + +// The describer class and result need two locals; reserve one spare slot. +constexpr jint kDescriptionLocalCapacity = 3; + +class LocalFrame { + public: + explicit LocalFrame(JNIEnv* env) : env_(env), pushed_(env->PushLocalFrame(kDescriptionLocalCapacity) == JNI_OK) {} + + ~LocalFrame() { + if (pushed_) { + env_->PopLocalFrame(nullptr); + } + } + + bool pushed() const { + return pushed_; + } + + private: + JNIEnv* env_; + bool pushed_; +}; + +std::string describe(JNIEnv* env, jthrowable throwable) { Review Comment: Added a short comment above `describe` explaining that lossy native wrappers and destructor logging may have only `what()`, not the carrier. Eager best-effort formatting and the placeholder fallback are unchanged. This core PR does not add carrier wrapping to the `jni:`/`jol:` filesystem callbacks. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
