From c683872bbcceb4063cfaf592ad02d149ad5d46bd Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Fri, 9 Feb 2018 10:59:15 +0100 Subject: [PATCH] Avoid server stacktraces on close On close, the socket is closed by the client, and the server process is killed. This leads to (expected) exceptions, that should not be printed. --- .../java/com/genymobile/scrcpy/ScrCpyServer.java | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/server/src/main/java/com/genymobile/scrcpy/ScrCpyServer.java b/server/src/main/java/com/genymobile/scrcpy/ScrCpyServer.java index f4dd793f..9895c535 100644 --- a/server/src/main/java/com/genymobile/scrcpy/ScrCpyServer.java +++ b/server/src/main/java/com/genymobile/scrcpy/ScrCpyServer.java @@ -1,6 +1,7 @@ package com.genymobile.scrcpy; import java.io.IOException; +import java.lang.reflect.InvocationTargetException; public final class ScrCpyServer { @@ -20,7 +21,7 @@ public final class ScrCpyServer { // synchronous screenEncoder.streamScreen(device, connection.getOutputStream()); } catch (IOException e) { - Ln.e("Screen streaming interrupted", e); + Ln.w("Screen streaming stopped"); } } } @@ -32,7 +33,7 @@ public final class ScrCpyServer { try { new EventController(device, connection).control(); } catch (IOException e) { - Ln.e("Exception from event controller", e); + Ln.w("Event controller stopped"); } } }).start(); @@ -60,6 +61,13 @@ public final class ScrCpyServer { Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() { @Override public void uncaughtException(Thread t, Throwable e) { + if (e instanceof AssertionError && e.getCause() instanceof InvocationTargetException) { + // WORKAROUND + // When we call a method of the framework by reflection, it may throw an InvocationTargetException + // (that we wrap into an AssertionError) if this process is being killed. + // To avoid the stacktrace on close, do not log these errors. + return; + } Ln.e("Exception on thread " + t, e); } });