From 400a1c69b198103feff9682895529529890c1acc Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Tue, 7 Feb 2023 22:58:46 +0100 Subject: [PATCH] Join all threads before end of main Some calls from separate threads may throw exceptions once the main() method has returned. --- .../src/main/java/com/genymobile/scrcpy/Controller.java | 8 +++++++- .../java/com/genymobile/scrcpy/DeviceMessageSender.java | 7 ++++++- server/src/main/java/com/genymobile/scrcpy/Server.java | 9 +++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/server/src/main/java/com/genymobile/scrcpy/Controller.java b/server/src/main/java/com/genymobile/scrcpy/Controller.java index 6147e36a..02684a1d 100644 --- a/server/src/main/java/com/genymobile/scrcpy/Controller.java +++ b/server/src/main/java/com/genymobile/scrcpy/Controller.java @@ -100,11 +100,17 @@ public class Controller { public void stop() { if (thread != null) { thread.interrupt(); - thread = null; } sender.stop(); } + public void join() throws InterruptedException { + if (thread != null) { + thread.join(); + } + sender.join(); + } + public DeviceMessageSender getSender() { return sender; } diff --git a/server/src/main/java/com/genymobile/scrcpy/DeviceMessageSender.java b/server/src/main/java/com/genymobile/scrcpy/DeviceMessageSender.java index 7ec4ab41..b0e2a388 100644 --- a/server/src/main/java/com/genymobile/scrcpy/DeviceMessageSender.java +++ b/server/src/main/java/com/genymobile/scrcpy/DeviceMessageSender.java @@ -66,7 +66,12 @@ public final class DeviceMessageSender { public void stop() { if (thread != null) { thread.interrupt(); - thread = null; + } + } + + public void join() throws InterruptedException { + if (thread != null) { + thread.join(); } } } diff --git a/server/src/main/java/com/genymobile/scrcpy/Server.java b/server/src/main/java/com/genymobile/scrcpy/Server.java index 81c3e813..ac5f0293 100644 --- a/server/src/main/java/com/genymobile/scrcpy/Server.java +++ b/server/src/main/java/com/genymobile/scrcpy/Server.java @@ -117,6 +117,15 @@ public final class Server { if (controller != null) { controller.stop(); } + + try { + initThread.join(); + if (controller != null) { + controller.join(); + } + } catch (InterruptedException e) { + // ignore + } } } }