Native: prohibit calling suspend functions from autoreleasepool {}
If a suspend function is called from `autoreleasepool {}` block, this
might cause the autoreleasepool frame to be removed earlier than
expected. See https://youtrack.jetbrains.com/issue/KT-50786 for more
details.
^KT-50786 Fixed
This commit is contained in:
committed by
Space
parent
55cfb3af20
commit
860f99482a
@@ -0,0 +1,30 @@
|
||||
import kotlinx.cinterop.*
|
||||
import kotlin.coroutines.*
|
||||
|
||||
open class EmptyContinuation(override val context: CoroutineContext = EmptyCoroutineContext) : Continuation<Any?> {
|
||||
companion object : EmptyContinuation()
|
||||
override fun resumeWith(result: Result<Any?>) { result.getOrThrow() }
|
||||
}
|
||||
|
||||
lateinit var continuation: Continuation<Unit>
|
||||
|
||||
suspend fun suspendHere(): Unit = suspendCoroutine { cont ->
|
||||
continuation = cont
|
||||
}
|
||||
|
||||
|
||||
fun startCoroutine(block: suspend () -> Unit) {
|
||||
block.startCoroutine(EmptyContinuation)
|
||||
}
|
||||
|
||||
fun main() {
|
||||
autoreleasepool {
|
||||
startCoroutine {
|
||||
autoreleasepool {
|
||||
suspendHere()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
continuation.resume(Unit)
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
import kotlinx.cinterop.*
|
||||
import kotlin.coroutines.*
|
||||
|
||||
open class EmptyContinuation(override val context: CoroutineContext = EmptyCoroutineContext) : Continuation<Any?> {
|
||||
companion object : EmptyContinuation()
|
||||
override fun resumeWith(result: Result<Any?>) { result.getOrThrow() }
|
||||
}
|
||||
|
||||
lateinit var continuation: Continuation<Unit>
|
||||
|
||||
suspend fun suspendHere(): Unit = suspendCoroutine { cont ->
|
||||
continuation = cont
|
||||
}
|
||||
|
||||
|
||||
fun startCoroutine(block: suspend () -> Unit) {
|
||||
block.startCoroutine(EmptyContinuation)
|
||||
}
|
||||
|
||||
inline fun <T> myAutoreleasepool(block: () -> T) = autoreleasepool(block)
|
||||
|
||||
fun main() {
|
||||
autoreleasepool {
|
||||
startCoroutine {
|
||||
myAutoreleasepool {
|
||||
suspendHere()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
continuation.resume(Unit)
|
||||
}
|
||||
Reference in New Issue
Block a user