From 41ccb5883ed3538b3f0a99c583beec16bfc893ce Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Sat, 28 Oct 2023 14:21:15 +0200 Subject: [PATCH] Force server exit at the end of main() 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 in some cases. So force the process to exit explicitly. PR #4213 --- .../main/java/com/genymobile/scrcpy/Server.java | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) 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); });