Skip to content

Commit

Permalink
Add more error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Mygod committed May 19, 2023
1 parent 53dffa8 commit 160dee7
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions lib/src/main/java/be/mygod/librootkotlinx/RootServer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,13 @@ class RootServer {
}
} else null
} ?: break
Logger.me.d("Received callback #$index: $result")
callback(input, result)
try {
Logger.me.d("Received callback #$index: $result")
callback(input, result)
} catch (e: Throwable) {
callback.cancel()
throw e
}
}
}

Expand All @@ -224,13 +229,14 @@ class RootServer {
callbackSpin()
if (active) throw UnexpectedExitException()
} catch (e: Throwable) {
Logger.me.d("Shutting down from worker due to error", e)
process.destroy()
if (e !is EOFException) throw e
} finally {
Logger.me.d("Waiting for exit")
withContext(NonCancellable) { errorReader.await() }
process.waitFor()
closeInternal(true)
closeInternal()
}
}
}
Expand Down Expand Up @@ -306,10 +312,9 @@ class RootServer {
}
}

private fun closeInternal(fromWorker: Boolean = false) = synchronized(callbackLookup) {
private fun closeInternal() = synchronized(callbackLookup) {
if (active) {
active = false
Logger.me.d(if (fromWorker) "Shutting down from worker" else "Shutting down from client")
try {
sendLocked(Shutdown())
output.close()
Expand All @@ -320,15 +325,14 @@ class RootServer {
}
Logger.me.d("Client closed")
}
if (fromWorker) {
for (callback in callbackLookup.valueIterator()) callback.cancel()
callbackLookup.clear()
}
for (callback in callbackLookup.valueIterator()) callback.cancel()
callbackLookup.clear()
}
/**
* Shutdown the instance gracefully.
*/
suspend fun close() {
Logger.me.d("Shutting down from client")
closeInternal()
val callbackListenerExit = callbackListenerExit ?: return
try {
Expand Down

0 comments on commit 160dee7

Please sign in to comment.