Refine implementation of init
This commit is contained in:
@@ -99,7 +99,7 @@ class RootServer {
|
|||||||
@Volatile
|
@Volatile
|
||||||
var active = false
|
var active = false
|
||||||
private var counter = 0L
|
private var counter = 0L
|
||||||
private lateinit var callbackListenerExit: Deferred<Unit>
|
private var callbackListenerExit: Deferred<Unit>? = null
|
||||||
private val callbackLookup = LongSparseArray<Callback>()
|
private val callbackLookup = LongSparseArray<Callback>()
|
||||||
private val mutex = Mutex()
|
private val mutex = Mutex()
|
||||||
|
|
||||||
@@ -195,16 +195,19 @@ class RootServer {
|
|||||||
* @param context Any [Context] from the app.
|
* @param context Any [Context] from the app.
|
||||||
* @param niceName Name to call the rooted Java process.
|
* @param niceName Name to call the rooted Java process.
|
||||||
*/
|
*/
|
||||||
suspend fun init(context: Context, niceName: String = "${context.packageName}:root") = try {
|
suspend fun init(context: Context, niceName: String = "${context.packageName}:root") {
|
||||||
val future = CompletableDeferred<Unit>()
|
withContext(Dispatchers.IO) {
|
||||||
callbackListenerExit = GlobalScope.async(Dispatchers.IO) {
|
|
||||||
try {
|
try {
|
||||||
doInit(context, niceName)
|
doInit(context, niceName)
|
||||||
future.complete(Unit)
|
} finally {
|
||||||
} catch (e: Throwable) {
|
try {
|
||||||
future.completeExceptionally(e)
|
readUnexpectedStderr()?.let { Logger.me.e(it) }
|
||||||
return@async
|
} catch (e: IOException) {
|
||||||
|
Logger.me.e("Failed to read from stderr", e) // avoid the real exception being swallowed
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
callbackListenerExit = GlobalScope.async(Dispatchers.IO) {
|
||||||
val errorReader = async(Dispatchers.IO) {
|
val errorReader = async(Dispatchers.IO) {
|
||||||
try {
|
try {
|
||||||
process.errorStream.bufferedReader().forEachLine(Logger.me::w)
|
process.errorStream.bufferedReader().forEachLine(Logger.me::w)
|
||||||
@@ -223,13 +226,6 @@ class RootServer {
|
|||||||
withContext(NonCancellable) { closeInternal(true) }
|
withContext(NonCancellable) { closeInternal(true) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
future.await()
|
|
||||||
} finally {
|
|
||||||
try {
|
|
||||||
readUnexpectedStderr()?.let { Logger.me.e(it) }
|
|
||||||
} catch (e: IOException) {
|
|
||||||
Logger.me.e("Failed to read from stderr", e) // avoid the real exception being swallowed
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -320,6 +316,7 @@ class RootServer {
|
|||||||
*/
|
*/
|
||||||
suspend fun close() {
|
suspend fun close() {
|
||||||
closeInternal()
|
closeInternal()
|
||||||
|
val callbackListenerExit = callbackListenerExit ?: return
|
||||||
try {
|
try {
|
||||||
withTimeout(10000) { callbackListenerExit.await() }
|
withTimeout(10000) { callbackListenerExit.await() }
|
||||||
} catch (e: TimeoutCancellationException) {
|
} catch (e: TimeoutCancellationException) {
|
||||||
|
|||||||
Reference in New Issue
Block a user