Patrick Steiger
11/26/2023, 5:49 PMcreateCoroutineUnintercepted()
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?Patrick Steiger
11/26/2023, 5:56 PMDmitry Khalanskiy [JB]
11/27/2023, 9:17 AMwithContext
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.Patrick Steiger
11/27/2023, 1:30 PMthread { 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 contextDmitry Khalanskiy [JB]
11/27/2023, 1:48 PMintercepted()
, 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.Patrick Steiger
11/28/2023, 12:51 AM