Skip to content

fix(jni): clear a pending exception thrown by app-supplied router callbacks - #743

Open
ayaangazali wants to merge 1 commit into
RunanywhereAI:mainfrom
ayaangazali:fix/jni-pending-exception
Open

fix(jni): clear a pending exception thrown by app-supplied router callbacks#743
ayaangazali wants to merge 1 commit into
RunanywhereAI:mainfrom
ayaangazali:fix/jni-pending-exception

Conversation

@ayaangazali

@ayaangazali ayaangazali commented Aug 18, 2026

Copy link
Copy Markdown
Contributor

What is wrong

Four JNI callbacks invoke app-supplied Java and never check for a pending exception:

site call
rac_hybrid_custom_filter_jni.cpp:102 predicate.evaluate(modelId)
rac_hybrid_device_state_jni.cpp:84 provider.isOnline()
rac_hybrid_device_state_jni.cpp:96 provider.batteryPercent()
rac_hybrid_device_state_jni.cpp:108 provider.isThermalThrottled()

Both objects are registered by the host app: a routing predicate and a device-state provider. For example:

return scope.env->CallBooleanMethod(a->provider, a->mid_is_online) != JNI_FALSE;

If that method throws, CallBooleanMethod returns a default and the exception stays pending on the thread. The garbage routing input is the small part. The real cost is that the next JNI call made by whoever we return into aborts the process with JNI called with pending exception. The hybrid router invokes these on ordinary routing decisions, so an NPE inside an app's isOnline() brings the process down somewhere unrelated to the app code that caused it.

Why these four and not others

I checked every JNI source that calls into Java. These two files were the only ones with Call*Method and zero exception checks:

file Call*Method ExceptionCheck / ExceptionOccurred
okhttp_transport_adapter.cpp 3 13
runanywhere_commons_jni.cpp 40 35
rac_cloud_stt_provider_jni.cpp 1 1
rac_hybrid_custom_filter_jni.cpp 1 0
rac_hybrid_device_state_jni.cpp 3 0

So this is a gap against the convention the rest of the JNI layer already follows, not a new policy.

What this does

Checks and clears, using the same idiom as okhttp_transport_adapter.cpp:

if (scope.env->ExceptionCheck() == JNI_TRUE) {
    scope.env->ExceptionClear();
    return /* documented fallback */;
}

The fallback per site is the value the function already returns when it cannot reach the JVM at all: keep the candidate, online, 100 percent battery, not throttled. So a throwing provider degrades exactly as far as an unreachable JVM already does, and no further.

Verification, and one limit I want to be straight about

These files are Android-only and are not part of the macOS commons build, so I could not run the usual cmake/ctest gate over them. What I did run is a real type check rather than a parse, using the JDK's own jni.h:

clang++ -fsyntax-only -std=c++20 \
  -I$JAVA_HOME/include -I$JAVA_HOME/include/darwin \
  -Icore/include -Icore/src \
  core/src/jni/rac_hybrid_device_state_jni.cpp     # rc=0
  core/src/jni/rac_hybrid_custom_filter_jni.cpp    # rc=0

I confirmed that check actually bites by introducing a deliberate typo, which it caught (error: use of undeclared identifier 'JNI_FALSE_TYPO'), so rc=0 means the edits type-check and not that the compiler skipped the file. The kotlin-android job is the authoritative gate here and will compile them for real.

No test added: reproducing this needs a Java provider that throws, driven through a live JNI attach on a device, which the commons suite cannot host.

One judgement call worth flagging: I clear the exception without logging it, matching the existing okhttp idiom, because neither file currently includes the logger and I did not want to widen the diff into a hot routing path. If you would rather these log once before clearing, say so and I will add it.

Summary by CodeRabbit

  • Bug Fixes
    • Improved resilience when custom predicates encounter exceptions.
    • Prevented device-state checks from failing when provider callbacks throw errors.
    • Preserved safe fallback values for connectivity, battery level, and thermal throttling status.

…lbacks

Four JNI callbacks invoke app code and never check for a pending exception:

  rac_hybrid_custom_filter_jni.cpp:102  predicate.evaluate(modelId)
  rac_hybrid_device_state_jni.cpp:84    provider.isOnline()
  rac_hybrid_device_state_jni.cpp:96    provider.batteryPercent()
  rac_hybrid_device_state_jni.cpp:108   provider.isThermalThrottled()

Both objects come from the host app: a routing predicate and a device-state
provider. When one throws, Call*Method returns a default and the exception stays
pending on that thread. The immediate cost is a garbage routing input, but the
real cost is the next JNI call made by whoever we return into, which aborts the
process with "JNI called with pending exception". The router calls these on
ordinary routing decisions, so an NPE inside an app's isOnline() takes the
process down somewhere unrelated.

Check and clear, then return the value each function already returns when it
cannot reach the JVM at all: keep the candidate, online, 100 percent battery,
not throttled. That keeps a throwing provider from changing routing behaviour
beyond what an unreachable JVM already does.

The idiom matches okhttp_transport_adapter.cpp, which guards its own
Call*Method sites the same way (and runanywhere_commons_jni.cpp has 35 such
checks); these two files were the only JNI sources calling into app code with
none.
Copilot AI lite review requested due to automatic review settings August 18, 2026 23:50

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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@coderabbitai

coderabbitai Bot commented Aug 18, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: ef3a90ea-88f0-414a-9985-691c902624e7

📥 Commits

Reviewing files that changed from the base of the PR and between 101bf2f and 48b8a22.

📒 Files selected for processing (2)
  • core/src/jni/rac_hybrid_custom_filter_jni.cpp
  • core/src/jni/rac_hybrid_device_state_jni.cpp

Included review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review.


📝 Walkthrough

Walkthrough

JNI callbacks now detect and clear exceptions from Kotlin or Java methods before returning. Custom filters retain candidates on predicate failure. Device-state callbacks return defined fallback values for online status, battery percentage, and thermal throttling.

Changes

JNI exception handling

Layer / File(s) Summary
Callback exception fallbacks
core/src/jni/rac_hybrid_custom_filter_jni.cpp, core/src/jni/rac_hybrid_device_state_jni.cpp
The custom filter clears predicate exceptions and retains the candidate. Device-state callbacks clear provider exceptions and return true, 100, or false for online status, battery percentage, or thermal throttling. Successful calls preserve their returned values.

Estimated code review effort: 2 (Simple) | ~10 minutes

Merge Risk: ⚪ Minimal · up to 48b8a

This localized change clears exceptions from app-supplied JNI callbacks and preserves existing fallback behavior; no actionable merge-blocking risk remains after normal checks and review.

Suggested reviewers: sanchitmonga22

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description explains the bug, affected callbacks, fallback behavior, and verification, but omits most required template sections. Add the required template headings and complete applicable checkboxes for Type of Change, Testing, Labels, Checklist, and Screenshots.
Docstring Coverage ⚠️ Warning Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies the JNI fix and pending exceptions from app-supplied router callbacks, matching the primary change.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@ayaangazali

Copy link
Copy Markdown
Contributor Author

Update on the verification caveat in the description: CI has now compiled these files for real, so the syntax-check limitation no longer applies.

android-arm64 passed on this PR (13m6s) and its log shows both changed translation units built with the NDK toolchain and linked:

[705/1056] Building CXX object core/src/jni/.../rac_hybrid_device_state_jni.cpp.o
[708/1056] Building CXX object core/src/jni/.../rac_hybrid_custom_filter_jni.cpp.o
[736/1056] Linking CXX shared library core/src/jni/librunanywhere_jni.so

kotlin-android is still running and will exercise the same objects through the AAR build. Every other check is green; nothing here is waiting on me.

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.

2 participants