Fix counter not protected by mutex

This commit is contained in:
Mygod
2021-05-20 17:10:40 -04:00
parent f55b42b6a1
commit 9c04af0d61

View File

@@ -245,13 +245,14 @@ class RootServer {
@Throws(RemoteException::class) @Throws(RemoteException::class)
suspend fun <T : Parcelable?> execute(command: RootCommand<T>, classLoader: ClassLoader?): T { suspend fun <T : Parcelable?> execute(command: RootCommand<T>, classLoader: ClassLoader?): T {
val future = CompletableDeferred<T>() val future = CompletableDeferred<T>()
@Suppress("UNCHECKED_CAST") val callback = mutex.withLock {
val callback = Callback.Ordinary(this, counter, classLoader, future as CompletableDeferred<Parcelable?>) @Suppress("UNCHECKED_CAST")
mutex.withLock { val callback = Callback.Ordinary(this, counter, classLoader, future as CompletableDeferred<Parcelable?>)
if (active) { if (active) {
callbackLookup[counter] = callback callbackLookup[counter] = callback
sendLocked(command) sendLocked(command)
} else future.cancel() } else future.cancel()
callback
} }
try { try {
return future.await() return future.await()
@@ -275,13 +276,14 @@ class RootServer {
else -> throw IllegalArgumentException("Unsupported channel capacity $it") else -> throw IllegalArgumentException("Unsupported channel capacity $it")
} }
}) { }) {
@Suppress("UNCHECKED_CAST") val callback = mutex.withLock {
val callback = Callback.Channel(this@RootServer, counter, classLoader, this as SendChannel<Parcelable?>) @Suppress("UNCHECKED_CAST")
mutex.withLock { val callback = Callback.Channel(this@RootServer, counter, classLoader, this as SendChannel<Parcelable?>)
if (active) { if (active) {
callbackLookup[counter] = callback callbackLookup[counter] = callback
sendLocked(command) sendLocked(command)
} else callback.finish.cancel() } else callback.finish.cancel()
callback
} }
try { try {
callback.finish.await() callback.finish.await()