From f50d579fd37f23c215f6a83e96d9f200de921992 Mon Sep 17 00:00:00 2001 From: Mygod Date: Sun, 22 Aug 2021 02:36:30 -0400 Subject: [PATCH] Wrap race condition in CancellationException --- .../main/java/be/mygod/librootkotlinx/RootServer.kt | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/mobile/src/main/java/be/mygod/librootkotlinx/RootServer.kt b/mobile/src/main/java/be/mygod/librootkotlinx/RootServer.kt index 95ac2bfb..7a8f624f 100644 --- a/mobile/src/main/java/be/mygod/librootkotlinx/RootServer.kt +++ b/mobile/src/main/java/be/mygod/librootkotlinx/RootServer.kt @@ -231,8 +231,12 @@ class RootServer { * Caller should check for active. */ private fun sendLocked(command: Parcelable) { - output.writeParcelable(command) - output.flush() + try { + output.writeParcelable(command) + output.flush() + } catch (e: IOException) { + if (e.isEBADF) throw CancellationException().initCause(e) else throw e + } Logger.me.d("Sent #$counter: $command") counter++ } @@ -300,8 +304,9 @@ class RootServer { sendLocked(Shutdown()) output.close() process.outputStream.close() + } catch (_: CancellationException) { } catch (e: IOException) { - if (!e.isEBADF) Logger.me.w("send Shutdown failed", e) + Logger.me.w("send Shutdown failed", e) } Logger.me.d("Client closed") }