Skip to content

Conversation

psiddh
Copy link
Contributor

@psiddh psiddh commented Oct 2, 2025

…variables - creates global references safe for persistence

findClassLocal() returns a local reference. Storing it in static auto exceptionClass = ... could result in potential 'invalid local reference:' as local references become invalid when the JNI frame ends

Test Plan:

Reviewers:

Subscribers:

Tasks:

Tags:

Summary

[PLEASE REMOVE] See CONTRIBUTING.md's Pull Requests for ExecuTorch PR guidelines.

[PLEASE REMOVE] If this PR closes an issue, please add a Fixes #<issue-id> line.

[PLEASE REMOVE] If this PR introduces a fix or feature that should be the upcoming release notes, please add a "Release notes: " label. For a list of available release notes labels, check out CONTRIBUTING.md's Pull Requests.

Test plan

[PLEASE REMOVE] How did you test this PR? Please write down any manual commands you used and note down tests that you have written if applicable.

@psiddh psiddh requested a review from kirklandsign as a code owner October 2, 2025 08:20
Copy link

pytorch-bot bot commented Oct 2, 2025

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/14744

Note: Links to docs will display an error until the docs builds have been completed.

❌ 1 New Failure, 2 Unrelated Failures

As of commit e6f4a47 with merge base f24351a (image):

NEW FAILURE - The following job has failed:

FLAKY - The following job failed but was likely due to flakiness present on trunk:

BROKEN TRUNK - The following job failed but was present on the merge base:

👉 Rebase onto the `viable/strict` branch to avoid these failures

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-cla meta-cla bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Oct 2, 2025
Copy link

github-actions bot commented Oct 2, 2025

This PR needs a release notes: label

If your change should be included in the release notes (i.e. would users of this library care about this change?), please use a label starting with release notes:. This helps us keep track and include your important work in the next release notes.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "release notes: none"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

Comment on lines 13 to 17
// Define the JavaClass wrapper
struct JExecutorchRuntimeException : public facebook::jni::JavaClass<JExecutorchRuntimeException> {
static constexpr auto kJavaDescriptor = "Lorg/pytorch/executorch/ExecutorchRuntimeException;";
};

Copy link
Contributor

Choose a reason for hiding this comment

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

What are your thoughts on moving this definition to the jni_helper.h file

// Find the Java ExecutorchRuntimeException class
static auto exceptionClass = facebook::jni::findClassLocal(
"org/pytorch/executorch/ExecutorchRuntimeException");
static const auto exceptionClass = JExecutorchRuntimeException::javaClassStatic();
Copy link
Contributor

@mergennachin mergennachin Oct 2, 2025

Choose a reason for hiding this comment

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

  auto env = facebook::jni::Environment::current();
  if (!env) {
    // Handle case where JNI environment is not available
    return;
  }

Copy link
Contributor Author

@psiddh psiddh Oct 3, 2025

Choose a reason for hiding this comment

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

Doesn't hurt to add defensive check (although checking for null is not very typical I think), but handling cases where it is null can be tricky, We can't talk to java layer as jni env is not valid, and it is not ideal to throw c++ exception from here.

// Find the Java ExecutorchRuntimeException class
static auto exceptionClass = facebook::jni::findClassLocal(
"org/pytorch/executorch/ExecutorchRuntimeException");
static const auto exceptionClass = JExecutorchRuntimeException::javaClassStatic();
Copy link
Contributor

Choose a reason for hiding this comment

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

Also, any thoughts on catching C++ exceptions? Something like this:

  try {
    static const auto exceptionClass = JExecutorchRuntimeException::javaClassStatic();
    // ... rest of the code
  } catch (const std::exception& e) {
    // Log the error or handle gracefully
    // Avoid throwing C++ exceptions across JNI boundary
  }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think catching C++ exceptions inside a helper like this can mask real initialization bugs and make debugging harder.

…variables - creates global references safe for persistence

findClassLocal() returns a local reference. Storing it in static auto exceptionClass = ... could result in potential 'invalid local reference:'
as local references become invalid when the JNI frame ends

Test Plan:

Reviewers:

Subscribers:

Tasks:

Tags:
@psiddh psiddh merged commit 3b16bc1 into pytorch:main Oct 6, 2025
127 of 130 checks passed
@psiddh
Copy link
Contributor Author

psiddh commented Oct 6, 2025

@pytorchbot cherry-pick --onto release/1.0 -c critical

pytorchbot pushed a commit that referenced this pull request Oct 6, 2025
#14744)

…variables - creates global references safe for persistence

findClassLocal() returns a local reference. Storing it in static auto
exceptionClass = ... could result in potential 'invalid local
reference:' as local references become invalid when the JNI frame ends

Test Plan:

Reviewers:

Subscribers:

Tasks:

Tags:

### Summary
[PLEASE REMOVE] See [CONTRIBUTING.md's Pull
Requests](https://github.com/pytorch/executorch/blob/main/CONTRIBUTING.md#pull-requests)
for ExecuTorch PR guidelines.

[PLEASE REMOVE] If this PR closes an issue, please add a `Fixes
#<issue-id>` line.

[PLEASE REMOVE] If this PR introduces a fix or feature that should be
the upcoming release notes, please add a "Release notes: <area>" label.
For a list of available release notes labels, check out
[CONTRIBUTING.md's Pull
Requests](https://github.com/pytorch/executorch/blob/main/CONTRIBUTING.md#pull-requests).

### Test plan
[PLEASE REMOVE] How did you test this PR? Please write down any manual
commands you used and note down tests that you have written if
applicable.

Co-authored-by: Github Executorch <[email protected]>
(cherry picked from commit 3b16bc1)
@pytorchbot
Copy link
Collaborator

Cherry picking #14744

The cherry pick PR is at #14811 and it is recommended to link a critical cherry pick PR with an issue. The following tracker issues are updated:

Details for Dev Infra team Raised by workflow job

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants