Skip to content

[jni] Fix interface implementation deadlock on the main thread #2032

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions pkgs/jni/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
and bootstrap jars [#2003](https://github.com/dart-lang/native/issues/2003)
- Added `JObject.isInstanceOf` which checks whether a `JObject` is an instance
of a java class.
- Fixed a [bug](https://github.com/dart-lang/native/issues/1908) where
Java interfaces implemented in on the main thread in Dart could deadlock when
invoked from the main thread outside the context of a Dart isolate.

## 0.14.0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,26 @@ private static final class DartImplementation {

private boolean built = false;
private final long isolateId;
private final boolean constructedOnMainThread;
private final HashMap<String, DartImplementation> implementations = new HashMap<>();
private final HashSet<String> asyncMethods = new HashSet<>();

private static boolean isOnMainThread() {
try {
Class<?> looper = Class.forName("android.os.Looper");
Method getMainLooper = looper.getMethod("getMainLooper");
Method getThread = looper.getMethod("getThread");
Thread mainThread = (Thread) getThread.invoke(getMainLooper.invoke(null));
return mainThread == Thread.currentThread();
} catch (Exception e) {
// Not on Android, so there is no concept of a "main" thread.
return false;
}
}

public PortProxyBuilder(long isolateId) {
this.isolateId = isolateId;
this.constructedOnMainThread = isOnMainThread();
}

private static String getDescriptor(Method method) {
Expand Down Expand Up @@ -121,7 +136,8 @@ private static native Object[] _invoke(
Object proxy,
String methodDescriptor,
Object[] args,
boolean isBlocking);
boolean isBlocking,
boolean mayEnterIsolate);

private static native void _cleanUp(long resultPtr);

Expand All @@ -139,6 +155,7 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
DartImplementation implementation = implementations.get(method.getDeclaringClass().getName());
String descriptor = getDescriptor(method);
boolean isBlocking = !asyncMethods.contains(descriptor);
boolean mayEnterIsolate = isOnMainThread() && constructedOnMainThread;
Object[] result =
_invoke(
implementation.port,
Expand All @@ -147,7 +164,8 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
proxy,
descriptor,
args,
isBlocking);
isBlocking,
mayEnterIsolate);
if (!isBlocking) {
return null;
}
Expand Down
20 changes: 18 additions & 2 deletions pkgs/jni/src/dartjni.c
Original file line number Diff line number Diff line change
Expand Up @@ -419,12 +419,14 @@ Java_com_github_dart_1lang_jni_PortProxyBuilder__1invoke(
jobject proxy,
jstring methodDescriptor,
jobjectArray args,
jboolean isBlocking) {
jboolean isBlocking,
jboolean mayEnterIsolate) {
CallbackResult* result = NULL;
if (isBlocking) {
result = (CallbackResult*)malloc(sizeof(CallbackResult));
}
if (isolateId != (jlong)Dart_CurrentIsolate_DL() || !isBlocking) {
if ((isolateId != (jlong)Dart_CurrentIsolate_DL() && !mayEnterIsolate) ||
!isBlocking) {
if (isBlocking) {
init_lock(&result->lock);
init_cond(&result->cond);
Expand Down Expand Up @@ -464,9 +466,23 @@ Java_com_github_dart_1lang_jni_PortProxyBuilder__1invoke(
destroy_cond(&result->cond);
}
} else {
// Flutter-specific: `mayEnterIsolate` is `true` when the proxy was
// constructed on the main thread and is being invoked on the main thread.
//
// When the current isolate is `null`, enter the main isolate that is pinned
// to the main thread first before invoking the `functionPtr`.
assert(Dart_CurrentIsolate_DL() == NULL ||
Dart_CurrentIsolate_DL() == (Dart_Isolate)isolateId);
bool mustEnterIsolate = Dart_CurrentIsolate_DL() == NULL && mayEnterIsolate;
Copy link
Contributor

Choose a reason for hiding this comment

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

LGTM. Though I just noticed another possible edge case.

If we're on the owner thread (mayEnterIsolate is true), and not entered into the target isolate, but instead entered into a different isolate. Then we should exit that other isolate, enter the target isolate, run the callback, exit the target isolate, then enter the other isolate. Of course, there's always the possibility that the other isolate is not owned by this thread, so could be entered by another thread while we're in the target isolate. What a mess.

Anyway, none of that is possible ATM, but could come up in future. Maybe just add an assert that Dart_CurrentIsolate_DL() is either null or isolateId?

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

if (mustEnterIsolate) {
Dart_EnterIsolate_DL((Dart_Isolate)isolateId);
}
result->object = ((jobject(*)(uint64_t, jobject, jobject))functionPtr)(
port, (*env)->NewGlobalRef(env, methodDescriptor),
(*env)->NewGlobalRef(env, args));
if (mustEnterIsolate) {
Dart_ExitIsolate_DL();
}
}
if (!isBlocking) {
// No result is created in this case, there is nothing to clean up either.
Expand Down