Skip to content

Commit

Permalink
Force server exit at the end of main()
Browse files Browse the repository at this point in the history
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 <#4213>
  • Loading branch information
rom1v committed Oct 31, 2023
1 parent 23e1160 commit 41ccb58
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion server/src/main/java/com/genymobile/scrcpy/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,22 @@ private static Thread startInitThread(final Options options) {
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);
});
Expand Down

0 comments on commit 41ccb58

Please sign in to comment.