Skip to content

JVM doesn't terminate when using Hybrids #25

@dreiss

Description

@dreiss

Issue description

In any program that uses HybridData, the JVM will not terminate when main returns. Calling System.exit still terminates cleanly. This appears to be because the Hybrid Destructor thread is not marked as a daemon thread. Calling sThread.setDaemon(true); resolves the issue, but I'm not sure if it can cause issues on Android.

Code example

Apply this patch:

diff --git a/host.gradle b/host.gradle
index 39f2d55..d88b96a 100644
--- a/host.gradle
+++ b/host.gradle
@@ -15,6 +15,8 @@
  */
 
 plugins {
+    id 'java'
+    id 'application'
     id 'java-library'
     id 'com.github.dcendents.android-maven' version '2.1'
     id 'com.jfrog.bintray' version '1.8.4'
@@ -47,3 +49,7 @@ POM_PACKAGING = 'jar'
 POM_ARTIFACT_ID = 'fbjni-java-only'
 
 apply from: rootProject.file('gradle/release.gradle')
+
+application {
+    mainClassName = 'com.facebook.jni.HybridRepro'
+}
diff --git a/java/com/facebook/jni/HybridRepro.java b/java/com/facebook/jni/HybridRepro.java
new file mode 100644
index 0000000..9b37056
--- /dev/null
+++ b/java/com/facebook/jni/HybridRepro.java
@@ -0,0 +1,14 @@
+package com.facebook.jni;
+
+import com.facebook.soloader.nativeloader.NativeLoader;
+import com.facebook.soloader.nativeloader.SystemDelegate;
+
+public class HybridRepro {
+  public static void main(String[] args) {
+    NativeLoader.init(new SystemDelegate());
+    new HybridData();
+    if (args.length > 0 && args[0].equals("exit")) {
+      System.exit(0);
+    }
+  }
+}

Running as ./gradlew -b host.gradle run --args nope causes the app to run forever.
Running as ./gradlew -b host.gradle run --args exit causes the app to terminate quickly.

System Info

Linux.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions