Skip to content

Display and control your Android device

Force server exit at the end of main()

Romain Vimont41ccb5883ed3
diff --git a/server/src/main/java/com/genymobile/scrcpy/Server.java b/server/src/main/java/com/genymobile/scrcpy/Server.java
index 2e6e1d4a..dc85c965 100644
--- a/server/src/main/java/com/genymobile/scrcpy/Server.java
+++ b/server/src/main/java/com/genymobile/scrcpy/Server.java
@@ -170,7 +170,22 @@ public final class Server {
return thread;
}
- public static void main(String... args) throws Exception {
+ public static void main(String... args) {
+ int status = 0;
+ try {
+ internalMain(args);
+ } catch (Throwable t) {
+ t.printStackTrace();
+ status = 1;
+ } finally {
+ // By default, the Java process exits when all non-daemon threads are terminated.
+ // The Android SDK might start some non-daemon threads internally, preventing the scrcpy server to exit.
+ // So force the process to exit explicitly.
+ System.exit(status);
+ }
+ }
+
+ private static void internalMain(String... args) throws Exception {
Thread.setDefaultUncaughtExceptionHandler((t, e) -> {
Ln.e("Exception on thread " + t, e);
});