From a10deadc3dc02c680262445b1b429a0df54b3f13 Mon Sep 17 00:00:00 2001 From: waicool20 Date: Tue, 21 Feb 2017 15:09:24 +0800 Subject: [PATCH] Ignoring stream closed error on session end --- .../com/waicool20/kaga/util/StreamGobbler.kt | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/main/kotlin/com/waicool20/kaga/util/StreamGobbler.kt b/src/main/kotlin/com/waicool20/kaga/util/StreamGobbler.kt index 08dc035..1e5d3d6 100644 --- a/src/main/kotlin/com/waicool20/kaga/util/StreamGobbler.kt +++ b/src/main/kotlin/com/waicool20/kaga/util/StreamGobbler.kt @@ -6,9 +6,16 @@ import java.io.InputStreamReader class StreamGobbler(val process: Process?) { fun run() { - val readOut = Thread { BufferedReader(InputStreamReader(process?.inputStream)).forEachLine(::println) } - val readErr = Thread { BufferedReader(InputStreamReader(process?.errorStream)).forEachLine(::println) } - readOut.start() - readErr.start() + val handler = Thread.UncaughtExceptionHandler { thread, throwable -> + if (throwable.message != "Stream closed") throw throwable // Ignore stream closed errors + } + with(Thread { BufferedReader(InputStreamReader(process?.inputStream)).forEachLine(::println) }) { + uncaughtExceptionHandler = handler + start() + } + with(Thread { BufferedReader(InputStreamReader(process?.errorStream)).forEachLine(::println) }) { + uncaughtExceptionHandler = handler + start() + } } }