Experimenting with intrinsics. I see on `createCor...
# coroutines
p
Experimenting with intrinsics. I see on
createCoroutineUnintercepted()
docs that when immediately calling
intercepted().resume(Unit)
on it, it’s guaranteed that execution of both the coroutine and the completion continuation happens in invocation context defined by the
ContinuationInterceptor
in completion continuation. That’s not really true, though. Coroutine will start in the context defined by interceptor, but if it changes context such that it finishes execution in a different thread, completion will be called in that different thread, not in the context defined by
ContinuationInterceptor
. Am I wrong?
d
In most places,
withContext
is treated as launching a separate coroutine in a separate context that the original one awaits, like
launch(newContext) { }.join()
, just with a few optimizations.
p
Hi Dmitry, thanks for responding, but I don’t quite get how this relates to the question. I’m using launch, yes, but just as an easy way to resume the suspended coroutine in another context — this could be
thread { c.resume(Unit) }
instead. In fact, just updated the playground example to use a thread instead. So it can’t be ‘another coroutine’ — it’s the same, but suspended and resumed in an arbitrary execution context
d
Oh, ok, I misunderstood the question, sorry. I can see where the confusion is coming from. It's not that the coroutine will be placed into the correct interceptor by
intercepted()
, but rather that the continuation will. And that's true: if the execution context changes, this means there's a new continuation that runs as part of this coroutine, and you need to call
intercepted()
on this new continuation as well. If you replace
c.resume(Unit)
with
c.intercepted().resume(Unit)
, the code will work.
p
Okay, that makes sense. Thanks! Still I’d push for adjustment of docs, since it states > Invocation of resume(Unit) on intercepted continuation guarantees that execution of both the coroutine and completion happens in the invocation context established by ContinuationInterception and seems that completion part is not 100% accurate. From that wording, I was under the impression that completion itself got intercepted as well with same interceptor from initial cont, but it seems that this fact of it being run in same exec context from ContinuationInterceptor is just a (weak) implication from the fact that the initial continuation is intercepted (weak because it depends on the suspend function not switching contexts and finishing in a different context) Being an intrinsic, should not matter much, but better docs are always a good thing :) Or maybe I’m just misreading the docs.