Marc Knaup
06/11/2019, 4:06 PMprivate object CurrentQueueDispatcher : CoroutineDispatcher() {
override fun dispatch(context: CoroutineContext, block: Runnable) {
dispatch_async(dispatch_get_current_queue()) {
block.run()
}
}
}
…
GlobalScope.launch(CurrentQueueDispatcher) { … }
Also, dispatch_async
or dispatch_sync
? The documentation doesn't state whether it's okay to block here or not.kotlin.native.IncorrectDereferenceException: illegal attempt to access non-shared CurrentQueueDispatcher.$dispatch$lambda-0$FUNCTION_REFERENCE$1@39254a8 from other thread
basher
06/11/2019, 5:00 PMMarc Knaup
06/11/2019, 5:01 PMbasher
06/11/2019, 5:05 PMMarc Knaup
06/11/2019, 5:06 PMbasher
06/11/2019, 5:06 PMWorker
+ runBlocking
to do coroutine-based work in a Worker
, being careful not to allow coroutine things to leave the worker and then transfer (or freeze) the result of that work back when you're doneMarc Knaup
06/11/2019, 5:11 PMprivate object BackgroundWorkerDispatcher : CoroutineDispatcher() {
private val worker = Worker.start()
override fun dispatch(context: CoroutineContext, block: Runnable) {
worker.execute(TransferMode.SAFE, { block }, { it.run() })
}
}
basher
06/11/2019, 5:11 PMblock
there is owned by the calling thread and coroutine internals. Passing it to the producer there will attempt to transfer it to the worker thread and crash (i expect)Marc Knaup
06/11/2019, 5:11 PMbasher
06/11/2019, 5:15 PMMarc Knaup
06/11/2019, 5:16 PMkotlin.IllegalStateException: Illegal transfer state
basher
06/11/2019, 5:17 PMDico
06/12/2019, 5:50 AMrunBlocking
as an event loopAndy Victors
06/12/2019, 7:27 AMMarc Knaup
06/12/2019, 11:00 AMAndy Victors
06/12/2019, 11:13 AMMarc Knaup
06/12/2019, 11:34 AMNSURLSession
delegate invocation and ktor coroutine invocation.svyatoslav.scherbina
06/13/2019, 7:51 AMworker.execute
resulting Future
leaking.Marc Knaup
06/13/2019, 12:19 PMbasher
06/13/2019, 1:38 PMMarc Knaup
06/13/2019, 1:39 PMFuture
so I don't see the leak 🤔
Maybe an unused Future leaks the result internally?svyatoslav.scherbina
06/13/2019, 2:23 PMFuture
is a resource. It leaks if the result is not consumed.basher
06/13/2019, 2:25 PMMarc Knaup
06/13/2019, 2:26 PMsvyatoslav.scherbina
06/13/2019, 2:31 PMEven if the result is Unit?AFAIK yes.
So how do I free a `Future`’s resources without blocking the thread?Once
future.state
is COMPUTED
you can free it with .consume {}
or .value
.
So you can add future to some registry which gets polled for finished futures.
See also https://github.com/JetBrains/kotlin-native/pull/2971 (will be included to 1.3.50).Marc Knaup
06/13/2019, 2:34 PMexecuteAfter
with a time interval of zero and don't need to take care of consuming the future anymore?basher
06/13/2019, 3:58 PMsvyatoslav.scherbina
06/14/2019, 8:03 AMSo I would basically useExactly.with a time interval of zero and don’t need to take care of consuming the future anymore?executeAfter